Skip to content

Instantly share code, notes, and snippets.

@evanshortiss
Last active March 22, 2019 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evanshortiss/83e63dc9cab83c7e7aba6780da3704bd to your computer and use it in GitHub Desktop.
Save evanshortiss/83e63dc9cab83c7e7aba6780da3704bd to your computer and use it in GitHub Desktop.
A quick web socket server setup in Node.js
/**
* Quick setup:
*
* $ mkdir /tmp/wss
* $ cd /tmp/wss
* $ npm init -f
* $ curl https://gist.githubusercontent.com/evanshortiss/83e63dc9cab83c7e7aba6780da3704bd/raw/ae5d532a371ef383d896f1663a454102cc1931c3/wss.js > wss.js
* $ node wss.js
*/
const WebSocket = require('ws');
const host = '0.0.0.0'
const port = 8080
const wss = new WebSocket.Server({
host,
port
});
wss.on('connection', function connection(ws) {
console.log('a client connected')
ws.on('message', function incoming(message) {
console.log('received payload: %s', message);
});
});
wss.on('listening', function listening() {
console.log(`started listening on ${host}:${port}`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment