const sseServer = new SSEServer({ url: '/files/:fileGuid/pull' }) const commentRegister = sseServer.register({ queryKeyword: 'comment', getRoomId(context: SSEClientContext) { return context.request.params.fileGuid }, async fetch (commentId: string, context: any) { const commentList = await getCommentList(commentId, context.roomId) return commentList } }); commentRegister.on('room:create', (roomId: string) => { messageBus.subscribe(`comment:${roomId}`) }) commentRegister.on('room:empty', (roomId: string) => { messageBus.unsubscribe(`comment:${roomId}`) }) messageBus.on('new-comment', comment => { commentRegister.send(comment) }) // for koa koaApp.use(sseServer.koaMiddleware()) // for express expressApp.use(sseServer.expressMiddleware()) // for http handler, like https://github.com/koajs/koa/blob/master/lib/application.js#L141 http.use(sseServer.callback()) // standalone sseServer.listen(9000)