用途:在 AdonisJS + Inertia 应用中实现站内通知与实时 SSE 推送。通知以行形式持久化在 notifications 表,并通过 SSE 实时推送到用户个人频道。
主要能力:Facteur(@facteurjs/adonisjs)作为通知框架,每种类型一个 Notification<User, Params> 类,配置 deliverBy: { database, transmit },并实现 asDatabaseMessage、asTransmitMessage 等按渠道方法。Transmit(@adonisjs/transmit)负责 SSE 传输,默认内存/单节点(config/transmit.ts 中 transport: null),横向扩展时换成分布式传输(如 Redis)。
使用方式:通知模块自带模型、transformer、控制器与路由:GET /notifications 列表与未读数;POST /notifications/:id/read 标记单条已读;POST /notifications/seen 全部标记已见;POST /notifications/read 全部标记已读。通知类放在 app/<mod>/notifications/<name>_notification.ts。用户路由通过 User.notificationTargets() 返回 database.notifiableId 与 transmit.channel(如 notifications/user-${this.id})。发送统一走领域事件:action 里 emitter.emit,监听器调用 await facteur.notification(SomeNotification).params({...}).to([user]).send()。Inertia 中间件在已认证时计算 unseenNotifications 计数供铃铛徽标首屏使用,之后由 SSE 保持同步。前端 SSE 客户端为惰性单例,一个 EventSource 连接 /__transmit/events,多频道复用。
适用场景:需要站内通知铃铛、未读计数与实时提醒的 AdonisJS 应用。
限制与注意:默认传输为单节点内存,多实例需换分布式驱动;transmit 消息应保持小体积,浏览器收到后重新拉取列表;不要在 action 中直接调用 facteur.notification(...).send(),应通过事件;不要在组件中硬编码频道名;渲染逻辑不要放在 transmit 消息里,数据库行才是持久副本。测试可用 Notification.create 直接插行测端点,端到端监听器测试先 mail.fake() 再 emit 并断言通知行存在。相关文档:Facteur(https://github.com/facteurjs/facteur)、AdonisJS Transmit(https://docs.adonisjs.com/guides/digging-deeper/transmit)、SSE 规范(https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events)。