WebSockets
RFC 6455 WebSocket server with rooms.
1 min readEdit this page
Setup
import { WSServer } from '@restjs/websocket'
const wss = new WSServer(app.getServer(), { path: '/ws' })
wss.on('connection', (client) => {
client.sendJSON({ type: 'welcome', id: client.id })
wss.join(client, 'chat')
client.on('message', (data) => {
wss.toRoomJSON('chat', { from: client.id, text: data.toString() })
})
})Client API
client.send('text')
client.sendJSON({ type: 'data' })
client.sendBinary(buffer)
client.close(1000, 'reason')
client.isOpenBroadcasting
wss.broadcastJSON(data)
wss.broadcastJSON(data, [excludeId])
wss.toRoomJSON('room', data)