npm start
(npm start -- -p 9898 -d 1000
) to start the server.
- websocket
http://localhost:3000
to subscribe/push events
port
/p
delay
/d
- minimum delay of each response/broadcast (milliseconds)
Start with following code on client side:
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
const socket = io('http://localhost:3000')
socket.on('state', (data) => {
console.log('state received once', data)
})
socket.on('data', (data) => {
console.log('data received on each client action', data)
})
Check client/client.js
file for more examples, including emitting events. Adapt the code into your client application.
You may want to adjust the server, by providing a custom reducer that updates the server state, just as the clients send events. Modify lib/state.js
.
Sent once initially, and then on demand. Includes initial state and currrent client ID.
Requesting on demand: socket.emit('state')
(request event). Needs to subscribe for state
as well (response event).
Clients may send data
event to notify the server about a change. It is then processed via the reducer and current state is updated (also available via state
events). data
events are broadcasted to all other clients.