simple-rebroadcaster
v0.0.3
Published
Simple service to re-broadcast websocket to all clients in a 'room'
Downloads
12
Readme
Simple Rebroadcaster
Simple server for websocket 'rooms' and 'clients'
Example
Using the server
import {start} from 'simple-rebroadcaster'
start(host, port, logger)Running the default server
Simply run with npm run start.
This will run server at environment variables APP_HOST and APP_PORT defaulting to "0.0.0.0" and 10000 respectively.
Clientside - Connecting, sending, listening
Simply connect as usual, including a roomId and clientId
// clientside javascript
const url = new URL('wss://your-hosted-server.com')
url.searchParams.append('roomId', 'some-room-id')
url.searchParams.append('clientId', 'some-client-id')
const socket = new WebSocket(url.toString())
socket.onopen = e => console.debug('socket.onopen', {e})
socket.onclose = e => console.debug('socket.onclose', {e})
socket.onerror = e => console.error('socket.onclose', {e})
socket.onmessage = e => console.debug('socket.onmessage', {e})When sending messages, anything that should be rebroadcast to any other clients in the room should be sent as json
socket.send(JSON.stringify({
some: 'arbitrary',
data: ['goes', 'here']
}))One exception is a ping/pong that can be used as a heartbeat for servers that are killed on inactivity
socket.send('ping')Messages are handled with the types described in the types file, the arbitrary client messages will look like this:
socket.on('message', e => {
// do nothing for heartbeat response
if(e.data === 'pong') return;
const data = JSON.parse(e.data)
// {
// type: 'client-event',
// data: {
// id: 'the-source-clients-id'
// data: {
// some: 'arbitrary',
// data: ['goes', 'here']
// }
// }
// }
})