This project demonstrates a simple Node.js WebSocket server and client setup where multiple WebSocket resource paths (e.g., /chat, /updates, /notifications) are handled separately.
It uses the lightweight ws library without any frameworks.
.
├── server.js # Node.js WebSocket Server handling multiple paths
└── client.js # Node.js WebSocket Client connecting to different paths
- Handle multiple WebSocket paths (
/chat,/updates,/notifications) - Clean separation of concerns per path
- Sends welcome message to client upon connection (e.g., "Connected to WebSocket resource: /chat")
- Full error handling for:
- Connection errors
- Unexpected server responses
- Message send failures
- Socket closures
- 404 handling for invalid paths
- Clear console logging for all important events
- Simple and minimal, ideal for learning or extending
-
Clone the repository:
git clone https://github.com/charithjayasanka/simple-websocket-multipath.git cd simple-websocket-multipath -
Install dependencies:
npm install ws
node server.jsThe server will start listening on ws://localhost:8080.
node client.jsThe client will connect to:
ws://localhost:8080/chatws://localhost:8080/updatesws://localhost:8080/notifications
and exchange simple test messages.
🚀 Server listening on port 8080
🔄 Incoming WebSocket upgrade request: /updates
✅ New connection on /updates from ::ffff:127.0.0.1
🔄 Incoming WebSocket upgrade request: /chat
✅ New connection on /chat from ::ffff:127.0.0.1
🔄 Incoming WebSocket upgrade request: /notifications
✅ New connection on /notifications from ::ffff:127.0.0.1
📩 Message on /updates: Hello /updates!
📩 Message on /chat: Hello /chat!
📩 Message on /notifications: Hello /notifications!
✅ Connected to /updates
📩 Received from /updates: 🟢 Connected to WebSocket resource: /updates
✅ Connected to /chat
📩 Received from /chat: 🟢 Connected to WebSocket resource: /chat
✅ Connected to /notifications
📩 Received from /notifications: 🟢 Connected to WebSocket resource: /notifications
📩 Received from /updates: Echo from /updates: Hello /updates!
📩 Received from /chat: Echo from /chat: Hello /chat!
📩 Received from /notifications: Echo from /notifications: Hello /notifications!
- If you connect to an unknown path (e.g.,
/invalid), the server will respond with a404 Not Foundand destroy the socket. - This project uses the bare WebSocket API (
ws) — no Socket.IO or other abstractions. - This project is intentionally minimal and can be extended for production-grade features (e.g., authentication, rooms, broadcasting).
Contributions, issues, and feature requests are welcome!
Feel free to open a pull request or an issue if you find a bug or want to suggest an improvement.
This project is licensed under the MIT License.