Hướng dẫn thử thách
1 / 1
WebSockets Quiz
To pass the quiz, you must correctly answer at least 18 of the 20 questions below.
# --quizzes--
## --quiz--
### --question--
#### --text--
Why does Node.js require an external library for WebSocket servers?
#### --distractors--
Node.js has a built-in WebSocket server but it requires extra configuration to enable.
---
Node.js only supports WebSockets when using the Express framework.
---
Node.js cannot support WebSockets at all, regardless of external packages.
#### --answer--
Node.js does not have a built-in WebSocket server.
### --question--
#### --text--
What are the three main parts of a Pub/Sub architecture?
#### --distractors--
Senders, queues, and receivers.
---
Producers, channels, and consumers.
---
Clients, servers, and databases.
#### --answer--
Publishers, topics, and subscribers.
### --question--
#### --text--
What communication model does HTTP follow?
#### --distractors--
A persistent bidirectional model.
---
A broadcast model.
---
A pub/sub model.
#### --answer--
The request-response model.
### --question--
#### --text--
Which WebSocket event fires when a new client connects to the server?
#### --distractors--
`connect`
---
`open`
---
`join`
#### --answer--
`connection`
### --question--
#### --text--
What is the main disadvantage of direct application-to-application communication compared to Pub/Sub?
#### --distractors--
Direct communication is always slower than Pub/Sub.
---
Direct communication prevents applications from sending more than one message.
---
Direct communication always requires a third-party authentication server.
#### --answer--
It creates an unnecessary dependency between the applications; a change or failure in one app can break the other.
### --question--
#### --text--
Which protocol does a WebSocket connection start with before upgrading?
#### --distractors--
WebSocket connections start with a dedicated WebSocket protocol from the very beginning.
---
WebSocket connections start with TCP, bypassing HTTP entirely.
---
Every WebSocket connection starts with FTP, then upgrades to WebSocket.
#### --answer--
Every WebSocket connection begins with HTTP.
### --question--
#### --text--
What method do you call to send data through a WebSocket connection to a client?
#### --distractors--
`socket.emit()`
---
`socket.write()`
---
`socket.push()`
#### --answer--
`socket.send()`
### --question--
#### --text--
What role does a topic play in a Pub/Sub system?
#### --distractors--
It stores messages permanently for future retrieval.
---
It authenticates publishers before they can send messages.
---
It directly connects publishers to specific subscribers.
#### --answer--
It acts as the middleman between publishers and subscribers.
### --question--
#### --text--
How does HTTP communication differ from WebSocket communication?
#### --distractors--
HTTP is bidirectional while WebSocket is unidirectional.
---
HTTP keeps a permanent connection, while WebSocket closes after each exchange.
---
HTTP and WebSocket both follow the same request-response pattern.
#### --answer--
HTTP follows `Client --> Request --> Server --> Response`, while WebSocket is `Client <--> Server`.
### --question--
#### --text--
Why should you check `client.readyState === WebSocket.OPEN` before broadcasting to connected clients?
#### --distractors--
To encrypt the message before sending it to each client.
---
To limit the number of messages sent per second to each client.
---
To verify the client has permission to receive the message.
#### --answer--
To avoid errors when sending to clients that have disconnected or are still connecting.
### --question--
#### --text--
What HTTP status code does the server send when it accepts a WebSocket upgrade request?
#### --distractors--
`200 OK`
---
`301 Moved Permanently`
---
`426 Upgrade Required`
#### --answer--
`101 Switching Protocols`
### --question--
#### --text--
Which of the following is a real-world use case for Pub/Sub?
#### --distractors--
A static web page that loads HTML from a server.
---
A command-line tool that processes files one at a time.
---
A relational database that stores and queries structured data.
#### --answer--
A notification system that delivers real-time alerts to all its subscribers.
### --question--
#### --text--
How do you create a WebSocket server on port `3000` using the `ws` library?
#### --distractors--
```js
const server = new WebSocket({ port: 3000 });
```
---
```js
const server = WebSocket.createServer(3000);
```
---
```js
const server = ws.listen(3000);
```
#### --answer--
```js
const server = new WebSocket.Server({ port: 3000 });
```
### --question--
#### --text--
Which WebSocket event is triggered when a connection closes?
#### --distractors--
`disconnect`
---
`end`
---
`terminate`
#### --answer--
`close`
### --question--
#### --text--
What is the Pub/Sub message flow?
#### --distractors--
A publisher sends to a subscriber --> the subscriber forwards to a topic --> the topic stores the message.
---
A topic sends a message --> publishers receive it --> publishers forward it to subscribers.
---
A subscriber connects to a publisher --> the publisher sends data --> the subscriber acknowledges receipt.
#### --answer--
A subscriber subscribes to a topic --> a publisher sends a message to that topic --> the messaging system delivers it to all subscribers of that topic.
### --question--
#### --text--
Which analogy best describes how HTTP and WebSockets differ?
#### --distractors--
HTTP is like a phone call, and WebSockets are like exchanging letters.
---
HTTP is like a chat room, and WebSockets are like a newspaper.
---
HTTP is like a live stream, and WebSockets are like a stored video.
#### --answer--
HTTP is like exchanging letters, and WebSockets are like a phone call.
### --question--
#### --text--
Which WebSocket event fires when a connected client sends data to the server?
#### --distractors--
`data`
---
`receive`
---
`incoming`
#### --answer--
`message`
### --question--
#### --text--
Which of the following correctly describes the WebSocket connection flow?
#### --distractors--
Client connects via TCP --> server sends data --> client receives --> connection closes.
---
Client sends data --> server requests upgrade --> connection opens --> client acknowledges.
---
Server initiates a connection --> client accepts --> both sides exchange data --> connection closes.
#### --answer--
Client connects via HTTP --> server responds with `101 Switching Protocols` --> connection stays open --> both sides exchange data at any time.
### --question--
#### --text--
How do WebSockets and Pub/Sub differ in a real-time application?
#### --distractors--
WebSockets handle authentication while Pub/Sub handles data storage.
---
WebSockets are for server-to-server communication while Pub/Sub is for client-to-server communication.
---
They are the same technology with different names.
#### --answer--
WebSockets provide the communication channel while Pub/Sub provides the messaging pattern.
### --question--
#### --text--
Which of the following is a real-world use case for WebSockets?
#### --distractors--
A static blog page displaying articles.
---
A server-side file rendering system.
---
A database that stores user records.
#### --answer--
A chat application.
Vượt qua bài kiểm tra hiện tại để mở khóa bài tiếp theo.
main.sql
UTF-8 • Tab Size: 2Kiểm tra bài:⌘↵