0
0
Node.jsframework~10 mins

ws library for WebSocket server in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ws library for WebSocket server
Start Node.js Server
Create WebSocket Server
Wait for Client Connection
Client Connects?
NoWait
Yes
Listen for Messages
Receive Message
Process & Respond
Client Disconnects?
NoListen for Messages
Yes
Close Connection
Stop Server or Wait for New Clients
The server starts, creates a WebSocket server, waits for clients, handles messages, and manages connections.
Execution Sample
Node.js
import { WebSocketServer } from 'ws';

const wss = new WebSocketServer({ port: 8080 });

wss.on('connection', ws => {
  ws.on('message', message => {
    ws.send(`Echo: ${message}`);
  });
});
This code creates a WebSocket server on port 8080 that echoes back any message it receives from clients.
Execution Table
StepEventActionMessage ContentServer Response
1Server StartWebSocket server created on port 8080--
2Client ConnectsConnection event triggered--
3Client Sends MessageMessage event triggeredHello ServerEcho: Hello Server
4Client Sends MessageMessage event triggeredHow are you?Echo: How are you?
5Client DisconnectsClose event triggered--
6No More ClientsServer waits for new connections--
💡 Execution stops when server is closed or no clients remain connected.
Variable Tracker
VariableStartAfter Client ConnectAfter Message 1After Message 2After Disconnect
wss.clients.size01110
ws.readyStateN/AOPENOPENOPENCLOSED
lastMessageN/AN/A"Hello Server""How are you?"N/A
Key Moments - 3 Insights
Why does the server keep running even after a client disconnects?
Because the server listens for new connections continuously as shown in execution_table step 6, it does not stop when one client disconnects.
What happens if the client sends multiple messages quickly?
Each message triggers the 'message' event separately, and the server responds to each as shown in steps 3 and 4.
Why is ws.readyState 'OPEN' after connection but 'CLOSED' after disconnect?
The readyState reflects the connection status: 'OPEN' means connected and able to send/receive, 'CLOSED' means connection ended, as tracked in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the server response when the client sends 'Hello Server'?
AEcho: Hello Server
BHello Server
CMessage received
DNo response
💡 Hint
Check execution_table row 3 under 'Server Response'
At which step does the client disconnect from the server?
AStep 2
BStep 3
CStep 5
DStep 6
💡 Hint
Look for 'Client Disconnects' event in execution_table
If a new client connects after step 5, how does wss.clients.size change?
ARemains 0
BChanges to 1
CChanges to 2
DChanges to -1
💡 Hint
Refer to variable_tracker for wss.clients.size changes after client connect
Concept Snapshot
ws library creates a WebSocket server with WebSocketServer({port}).
Listen for 'connection' events to handle clients.
Use ws.on('message') to receive messages.
Respond with ws.send().
Server runs continuously, handling multiple clients.
Close connections with ws.close().
Full Transcript
This visual execution trace shows how to create a WebSocket server using the ws library in Node.js. The server starts and listens on port 8080. When a client connects, the server triggers a connection event and listens for messages from that client. Each message received triggers a message event, and the server responds by sending back an echo of the message. When the client disconnects, the server triggers a close event but continues running to accept new clients. Variables like the number of connected clients and connection state update accordingly. This step-by-step flow helps beginners see how the server handles connections and messages in real time.