Real-Time

WebSockets

RFC 6455 WebSocket server with rooms.

1 min readDocumentationEdit this page

Setup#

typescript
import { WSServer } from '@AxilJS/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#

typescript
client.send('text')
client.sendJSON({ type: 'data' })
client.sendBinary(buffer)
client.close(1000, 'reason')
client.isOpen

Broadcasting#

typescript
wss.broadcastJSON(data)
wss.broadcastJSON(data, [excludeId])
wss.toRoomJSON('room', data)

Help improve the documentation

AxilJS is open source and documentation improvements are welcome.

AxilJS DocumentationMIT License · Built by SyntaxilitY