WebSockets and Real-Time Applications in Deno Quiz Quiz

Explore your understanding of WebSockets fundamentals and real-time application development using Deno. This quiz covers key concepts, connection handling, message exchange, scalability, and security considerations relevant to modern web communication.

  1. WebSocket Protocol Basics

    What is the primary purpose of the WebSocket protocol in real-time web applications?

    1. Rendering static HTML pages at high speed
    2. Securing data with encryption only
    3. Batch processing of HTTP requests
    4. Enabling two-way, persistent communication between client and server

    Explanation: WebSockets are designed to allow bidirectional, persistent connections, making real-time interactions between client and server possible. Encryption is often used, but it's not the main purpose of the protocol. Batch processing of HTTP requests and rendering static HTML pages are unrelated to WebSocket functionalities. Those options mix up concepts from other web technologies.

  2. WebSocket Connection in Deno

    In Deno, which method is primarily used to establish a WebSocket connection from the client-side?

    1. new WebSocket('ws://example.com')
    2. fetch('ws://example.com')
    3. createSocket('ws://example.com')
    4. startWSConnection('ws://example.com')

    Explanation: The standard way to open a WebSocket connection is by creating a new WebSocket object with the server URL. The fetch API is designed for HTTP requests, not WebSocket connections. The other options, createSocket and startWSConnection, are not methods recognized in typical web or Deno environments and could be mistaken for custom or non-existent APIs.

  3. Real-Time Features

    Which scenario best demonstrates a real-time application benefitting from WebSockets?

    1. A site map generator for search engine indexing
    2. A chat app where messages appear instantly for all users
    3. A batch file uploader that processes files overnight
    4. A static blog displaying pre-written articles

    Explanation: A chat application relies on real-time communication, which WebSockets facilitate by enabling instant data exchange. Static blogs do not require real-time updates. Batch uploads occur in bulk without the need for ongoing two-way communication. A site map generator is an offline process and doesn’t benefit from real-time data transfer.

  4. WebSocket Closing Handling

    When a client disconnects from a WebSocket server, which event should be handled to clean up resources on the server?

    1. close
    2. timeout
    3. retry
    4. refresh

    Explanation: The 'close' event is triggered when a WebSocket connection is closed, which is the correct moment to clean up any resources. 'Retry' is not a standard WebSocket event. 'Timeout' might relate to other protocols and isn’t directly tied to WebSockets. 'Refresh' is unrelated to connection lifecycle events.

  5. WebSocket URL Scheme

    Which URI scheme is used to indicate a secure WebSocket connection?

    1. http://
    2. ftp://
    3. ws://
    4. wss://

    Explanation: Secure WebSockets use the wss:// prefix, functioning similarly to how https:// secures HTTP traffic. ws:// is the non-secure scheme. http:// refers to regular web pages without persistent connections, while ftp:// is used for file transfer, not WebSockets.

  6. Message Format

    If a Deno WebSocket server receives a message from the client, in what format does the message typically arrive?

    1. As a string or binary data
    2. Only as a formatted JSON object
    3. Always as a CSV file
    4. Strictly as an HTML page

    Explanation: WebSocket messages can be transmitted as either string (text) or binary data, providing flexibility. They are not limited to JSON; that is just a common convention for data exchange. HTML pages and CSV files are data formats that are unrelated and not required by the protocol.

  7. Broadcasting in Real-Time Apps

    What is required for a Deno WebSocket server to broadcast a message to all connected clients?

    1. Clients must refresh their page manually to receive updates
    2. It must use the broadcast() method built into standard WebSocket API
    3. Messages must be routed through email servers
    4. It must track and iterate through all active client connections

    Explanation: Broadcasting is typically done by keeping track of connected clients and manually sending the message to each. There is no built-in broadcast() method in the standard WebSocket API. Manual refreshes defeat the purpose of live updates, and email servers are not involved in real-time messaging.

  8. WebSocket Security

    Which security measure is recommended for protecting data exchanged over a WebSocket connection?

    1. Reducing message size to avoid interception
    2. Using wss:// to encrypt the connection
    3. Disabling authentication for faster speed
    4. Serving connections on port 21

    Explanation: Encrypting communications via wss:// helps protect data in transit against interception. Disabling authentication introduces risk rather than mitigating it. While reducing message size may be efficient, it does not directly enhance security. Port 21 is traditionally used for file transfer and is not associated with WebSockets.

  9. WebSocket Scalability

    What is a major challenge when scaling WebSocket-based real-time applications?

    1. Storing static HTML pages efficiently
    2. Handling only one connection per server
    3. Ensuring all clients receive messages when servers run across multiple processes
    4. Using UDP instead of TCP

    Explanation: Scaling requires coordination so that all clients, even those connected to different server processes, receive real-time messages. Handling just one connection per server is not scalable or practical. Static HTML storage does not pertain to real-time aspects. UDP is not used by the WebSocket protocol, which operates over TCP.

  10. Heartbeat Mechanisms

    Why are heartbeat or ping messages valuable in WebSocket real-time applications?

    1. They speed up HTML rendering
    2. They compress WebSocket frames
    3. They increase message encryption strength
    4. They help detect disconnections between client and server

    Explanation: Heartbeat or ping messages allow either side to check if the connection is still active, making it easier to handle unexpected disconnections. They do not affect encryption strength, HTML rendering, or compression directly. The other options are incorrect as they confuse the purpose of heartbeat messages.