UNPKG

768 BJavaScriptView Raw
1
2const ChatService = require('../index')
3
4const port = 8000
5
6function onConnect (service, id) {
7 // Assuming that auth data is passed in a query string.
8 let { query } = service.transport.getHandshakeData(id)
9 let { userName } = query
10 // Actually check auth data.
11 // ...
12 // Return a promise that resolves with a login string.
13 return Promise.resolve(userName)
14}
15
16const chatService = new ChatService({port}, {onConnect})
17
18process.on('SIGINT', () => chatService.close().finally(() => process.exit()))
19
20// It is an error to add the same room twice. The room configuration
21// and messages will persist if redis state is used.
22chatService.hasRoom('default').then(hasRoom => {
23 if (!hasRoom) {
24 return chatService.addRoom('default', { owner: 'admin' })
25 }
26})