This repo contains a tech demo of WebSckets based on http://codular.com/node-web-sockets tutorial using two different WebSocket npm packages.
https://www.npmjs.com/package/ws https://www.npmjs.com/package/websocket
In addition, ws
version is compatible out of the box with Java WebSocket client outlined in https://www.eclipse.org/jetty/documentation/9.3.x/jetty-websocket-client-api.html tutorial. Files from the tutorial are also present in this repository.
- Run
npm install
- Run
node websockets.js
or 'nodews.js
- Instantiate WebSocket in your browser console
var ws = new WebSocket('ws://localhost:3001', 'echo-protocol');
See java folder
- Bind server response message to console.log
ws.onmessage = function(event){
console.log(JSON.stringify(event.data));
};
- Send message to server
ws.send("message")
You can easily make this WebSocket server accessible over LAN or WAN by routing port traffic to the host machine (3000 in my case). I was able run steps 3-5 on multiple comuputers connecting to a single LAN node.