Comparing WebSocket and Socket.IO on Node.js with Express.js - example server-side and client-side code to see the basic usage of both WebSocket and Socket.IO in an Express.js app, with info on how to compare the actual network traffic (which is commonly misunderstood by many online sources).
See those Stack Overflow answers where this code was used:
- Differences between socket.io and websockets
- socket.io no communication between server and client
- why web socket behave differently on nodejs ?
- getting an error when trying to use socket.io
- How to use Socket.io combined with Express.JS
- Node.js long polling event loop breaks the whole code
Install:
npm i -g websocket-vs-socket.io
Run the server:
websocket-vs-socket.io
Open http://localhost:3001/ in your browser, open developer tools with Shift+Ctrl+I, open the Network tab and reload the page with Ctrl+R to see the network traffic for the WebSocket version.
Open http://localhost:3002/ in your browser, open developer tools with Shift+Ctrl+I, open the Network tab and reload the page with Ctrl+R to see the network traffic for the Socket.IO version.
Uninstall the server:
npm rm -g websocket-vs-socket.io
For more options, see Installation below.
More options to install the server.
Installing globally with npm:
# install globally with npm:
npm i -g websocket-vs-socket.io
# run the server:
websocket-vs-socket.io
# uninstall:
npm rm -g websocket-vs-socket.io
Installing locally with npm:
# create a directory:
mkdir ~/websocket-vs-socket.io
cd ~/websocket-vs-socket.io
# install the module:
npm i websocket-vs-socket.io
# run the server:
./node_modules/.bin/websocket-vs-socket.io
# uninstall the module:
npm rm websocket-vs-socket.io
# or remove the entire directory:
rm -rvf ~/websocket-vs-socket.io
You can clone the git repo and install npm dependencies:
# clone the git repo:
git clone [email protected]:rsp/node-websocket-vs-socket.io.git
cd node-websocket-vs-socket.io
# install dependencies:
npm i
# run the server:
./ws-vs-si.js
Alternatively, instead of cloning the repo you can download a ZIP file:
# download and unzip the zip file:
wget https://github.com/rsp/node-websocket-vs-socket.io/archive/master.zip
unzip master.zip
cd node-websocket-vs-socket.io-master
# install dependencies:
npm i
# run the server:
./ws-vs-si.js
You can build and run own docker image running from "root" repo folder
docker-compose -f ./docker/docker-compose.yml build
docker-compose -f ./docker/docker-compose.yml up -d
You start the server with either
websocket-vs-socket.io
(if you installed from npm) or
./ws-vs-si.js
(if you installed from the GitHub repo).
See Installation above for details.
When the server is running, you can open in your browser:
- http://localhost:3001/ - WebSocket version
- http://localhost:3002/ - Socket.IO version
- http://localhost:3002/forced - Socket.IO version (forced websockets)
In Firefox or Chrome, you can open developer tools with Shift+Ctrl+I, open the Network tab and reload the page with Ctrl+R to see the network traffic for both versions.
For any bug reports or feature requests please post an issue on GitHub.
Rafał Pocztarski - https://github.com/rsp
- Miloš Popović (improved documentation in PR #1)
- Andrei Derevyagin (added Docker support in PR #3)
- Orkun Tümer (added Socket.io forced WebSocket feature in PR #4, closing Issue #2)
Special thanks to:
for comments and suggestions.
MIT License (Expat). See LICENSE.md for details.