uNetworking / uWebSockets.js

μWebSockets for Node.js back-ends :metal:
Apache License 2.0
7.85k stars 570 forks source link

pub/sub browser client example? #421

Closed ronag closed 3 years ago

ronag commented 3 years ago

Would be nice with an example on how one could interact with pub/sub over a browser client websocket?

hst-m commented 3 years ago

Client:

ws=new WebSocket('ws://localhost:3000')
ws.onmessage=e=>console.log(e.data)
ws.onopen=e=>ws.send('Hello all connected clients')

Server:

app=require('uWebSockets.js').App().ws('/',{
   open:ws=>ws.subscribe('all'),
   message:(ws,ab)=>app.publish('all',ab)
}).listen(3000,t=>{})