What is a common alternative to Server-Sent Events (SSE) for real-time communication in Node.js?
WebSockets are a common alternative to SSE. They allow full-duplex communication between client and server, meaning both can send messages independently at any time.
Click to reveal answer
beginner
How do WebSockets differ from Server-Sent Events in terms of communication direction?
Server-Sent Events allow only server-to-client streaming, while WebSockets support two-way communication, letting both client and server send messages anytime.
Click to reveal answer
intermediate
Which Node.js library is popular for implementing WebSockets easily?
Socket.IO is a popular Node.js library that simplifies WebSocket implementation and provides fallback options for older browsers.
Click to reveal answer
intermediate
What is a key advantage of using WebSockets over Server-Sent Events?
WebSockets provide bidirectional communication, which is useful for chat apps or games where both client and server need to send messages actively.
Click to reveal answer
advanced
Can Server-Sent Events handle binary data like WebSockets?
No, Server-Sent Events only support text data, while WebSockets can send both text and binary data efficiently.
Click to reveal answer
Which technology allows two-way communication between client and server?
AREST API
BServer-Sent Events
CHTTP Polling
DWebSockets
✗ Incorrect
WebSockets enable two-way communication, unlike Server-Sent Events which only allow server-to-client streaming.
Which Node.js library helps implement WebSockets easily?
ASocket.IO
BAxios
CExpress
DMongoose
✗ Incorrect
Socket.IO is designed to simplify WebSocket usage and provide fallback options.
Server-Sent Events can only send which type of data?
ABinary data
BText data
CImages
DAudio streams
✗ Incorrect
SSE supports only text data streams from server to client.
Which is NOT a feature of WebSockets?
AFull-duplex communication
BSupports binary data
COnly server-to-client messages
DLow latency messaging
✗ Incorrect
WebSockets support two-way communication, not just server-to-client.
What fallback does Socket.IO provide if WebSockets are not supported?
AHTTP Polling
BFTP
CSMTP
DWebRTC
✗ Incorrect
Socket.IO can fall back to HTTP Polling when WebSockets are unavailable.
Explain the main differences between Server-Sent Events and WebSockets.
Think about communication direction and data types.
You got /4 concepts.
Describe how you would implement a real-time chat feature in Node.js using an alternative to Server-Sent Events.
Focus on bidirectional communication and libraries.
You got /4 concepts.
Practice
(1/5)
1. What is a key difference between Server-Sent Events (SSE) and WebSockets in Node.js?
easy
A. SSE only allows server-to-client messages, while WebSockets allow two-way communication.
B. SSE supports two-way communication, but WebSockets only send data from server to client.
C. Both SSE and WebSockets only allow server-to-client communication.
D. WebSockets are slower than SSE for sending messages.
Solution
Step 1: Understand SSE communication direction
Server-Sent Events send updates only from the server to the client, not the other way around.
Step 2: Understand WebSocket communication direction
WebSockets allow full two-way communication between client and server, enabling interactive real-time data exchange.
Final Answer:
SSE only allows server-to-client messages, while WebSockets allow two-way communication. -> Option A
Quick Check:
Communication direction = A [OK]
Hint: Remember: SSE is one-way, WebSocket is two-way [OK]
Common Mistakes:
Thinking SSE supports two-way communication
Confusing WebSocket speed with direction
Assuming both are identical in communication
2. Which of the following is the correct way to create a WebSocket server using the 'ws' library in Node.js?
easy
A. const ws = require('ws'); const server = new ws.Server(8080);
B. const WebSocket = require('ws'); const server = new WebSocket.Server({ port: 8080 });
C. import ws from 'ws'; const server = ws.createServer(8080);
D. const ws = require('ws'); const server = ws.createServer({ port: 8080 });
Solution
Step 1: Identify correct import and usage
The 'ws' library is imported as WebSocket, and the server is created with new WebSocket.Server({ port: 8080 }).
Step 2: Check syntax correctness
The other options contain syntax errors, such as incorrect constructor arguments, non-existent methods like 'createServer', or invalid import styles.
Final Answer:
const WebSocket = require('ws'); const server = new WebSocket.Server({ port: 8080 }); -> Option B
Quick Check:
Correct WebSocket server syntax = C [OK]
Hint: Use 'new WebSocket.Server' with correct import [OK]
Common Mistakes:
Using wrong class name or method
Mixing import styles incorrectly
Calling non-existent createServer method
3. Given this Node.js WebSocket server code snippet, what will be logged when a client sends 'Hello'?
A. The 'message' event handler should be synchronous.
B. The WebSocket server port is missing.
C. The 'connection' event is misspelled.
D. The variable 'message' is undefined inside the message event handler.
Solution
Step 1: Check variable names in event handler
The message event handler uses parameter 'msg' but tries to send 'message', which is undefined.
Step 2: Confirm other code parts
The port is correctly set, 'connection' event is spelled correctly, and async/sync is not an error here.
Final Answer:
The variable 'message' is undefined inside the message event handler. -> Option D
Quick Check:
Variable name mismatch = A [OK]
Hint: Match parameter names exactly inside handlers [OK]
Common Mistakes:
Using wrong variable name inside callback
Assuming port or event name errors
Confusing async requirement for message handler
5. You want to replace Server-Sent Events with WebSockets in a Node.js app to allow clients to send messages back to the server. Which approach best achieves this?
hard
A. Use the 'ws' library to create a WebSocket server that listens for 'message' events from clients and broadcasts updates.
B. Use the 'http' module to send periodic SSE messages and ignore client messages.
C. Use the 'express' module to handle SSE and add a REST API for client messages.
D. Use the 'net' module to create a TCP server for SSE communication.
Solution
Step 1: Identify WebSocket capabilities
WebSockets allow two-way communication, so using the 'ws' library to listen for client messages fits the requirement.
Step 2: Evaluate other options
The 'net', 'http', and 'express' with REST API options do not provide true two-way real-time communication; they rely on polling, REST calls, or low-level TCP unsuitable for replacing SSE with bidirectional messaging.
Final Answer:
Use the 'ws' library to create a WebSocket server that listens for 'message' events from clients and broadcasts updates. -> Option A
Quick Check:
Two-way real-time communication = D [OK]
Hint: Choose WebSocket server for two-way real-time messaging [OK]
Common Mistakes:
Using SSE for two-way communication
Confusing HTTP REST with real-time sockets
Using low-level TCP instead of WebSocket
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.