Skip to content

Instantly share code, notes, and snippets.

@supershabam
Last active December 27, 2015 20:09
Show Gist options
  • Save supershabam/7382515 to your computer and use it in GitHub Desktop.
Save supershabam/7382515 to your computer and use it in GitHub Desktop.
A pure WebSocket echo server (like echo.websocket.org)
// chatroom.js
var WSServer = require('ws').Server
, wss = new WSServer()
// handle only pure clean websockets
wss.on('connection', function(ws) {
// broadcast new connection count
wss.sockets.forEach(function(s) {
s.send(wss.sockets.length)
})
// broadcast incoming message
ws.on('message', function(message) {
wss.sockets.forEach(function(s) {
s.send(message)
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment