Last active
November 26, 2019 03:49
-
-
Save wanming/77ea61815a8647fd5453e41e624b68ad to your computer and use it in GitHub Desktop.
sse server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
attaches sse.io to a plain Node.JS HTTP server
standalone
In conjunction with Express
In conjunction with Koa