This is a simple POC that shows how to broadcast "instant" messages across browsers / clients using WebSockets and avoiding any polling. The goal of building this POC was just test WebSockets and increase the knowledge about this technology.
- Install NodeJS (version 12+), we recommend doing that using NVM
- Install typescript utils (recommended globally)
npm install -g ts-node
npm install -g typescript
- Clone this repo:
git clone git@github.com:jordicenzano/websocket-chat.git
- Install packages
cd server
npm install
cd ..
- (ONLY if you want to use the secure version)Create self signed certificate and key running:
cd server/scripts
./createSelfSignedCert.sh
cd ../..
- Start the backend webSocket server code in a place where the clients can reach, for instance:
ts-node ./server/src/ws-chat-server.ts -p 8080
The previous command will run the server in your computer on port 8080
-
Load this webpage in your device WebSocketChat
-
Type
ws://localhost:8080
(OR your server location) asWS Server URL
(*) -
Type
myroom
(OR the room name you want) inRoom ID
-
Type
RANDOM
(OR the ID you want) inName
-
Press the "Connect" button
-
Repeat steps 2 to 6 with as many clients as you want
-
Send a messages
Tested with Chrome in MACOS.
(*) Some browsers (like Safari) can throw an error if you try to connect an NON encrypted WebSocket (ws:) from a HTTPS page.