WebSockets (real-time apps) Quiz

Challenge your understanding of WebSocket technology and its role in developing real-time applications. Explore key concepts such as connection handling, advantages over traditional protocols, message formats, lifecycle events, and security considerations related to WebSockets.

  1. Persistent Connection Mechanism

    Which statement best describes how WebSockets maintain communication between a client and server in a real-time chat application?

    1. WebSockets close and reopen the connection with every message sent.
    2. WebSockets rely on sending discrete HTTP requests for each message.
    3. WebSockets require manual polling to receive data from the server.
    4. WebSockets use a single, persistent connection that remains open for continuous data exchange.

    Explanation: WebSockets establish a single, persistent connection that allows full-duplex communication between client and server, ideal for real-time scenarios like chat applications. Unlike traditional HTTP, which needs to set up a new connection for each message, WebSockets avoid overhead by keeping one connection open. Option B is incorrect because repeated HTTP requests are not used. Option C is wrong since repeatedly opening and closing is inefficient and not how WebSockets work. Option D is incorrect as polling is not needed; data can be received any time once the connection is established.

  2. Protocol Advantages

    What is a major advantage of using WebSockets instead of HTTP long polling for a live data dashboard?

    1. WebSockets always use encrypted communication by default.
    2. WebSockets require no initial handshake to establish a connection.
    3. WebSockets prevent any kind of data loss during network failures.
    4. WebSockets eliminate the need for periodic client requests, reducing network overhead.

    Explanation: WebSockets allow continuous communication without repeated requests, lowering the bandwidth and latency typical in HTTP long polling. Option B is incorrect because WebSockets may use encryption but not always by default. Option C is misleading; while WebSockets are efficient, they do not guarantee prevention of data loss if the network fails. Option D is wrong because WebSockets do require an initial handshake to establish the connection.

  3. Message Structure

    When sending messages over WebSockets, which data format is commonly used for exchanging information in a collaborative drawing app?

    1. HTML
    2. SOAP
    3. BMP
    4. JSON

    Explanation: JSON is commonly used for WebSocket messages because it is lightweight, easy to parse, and language-independent, making it ideal for transmitting real-time application events like drawing actions. HTML is primarily used for structuring web pages, not efficient data serialization. BMP refers to an image file format and is too bulky for real-time data exchange. SOAP is a protocol used in web services, not typically employed for WebSocket message payloads.

  4. Connection Lifecycle Events

    Which event occurs when a WebSocket client receives a message from the server during live multiplayer gameplay?

    1. onopen
    2. onclose
    3. onsend
    4. onmessage

    Explanation: The onmessage event is triggered when the client receives data from the server, which is crucial for real-time updates in multiplayer games. Option B, onclose, is only used when a connection closes. Onopen, option C, fires when the connection is first established, not when messages are received. Option D, onsend, is not a standard WebSocket event.

  5. Security and Protocol Usage

    Which WebSocket protocol identifier should be used to ensure encrypted communication between a browser and server in a stock ticker app?

    1. wss://
    2. https://
    3. sock://
    4. ws:/

    Explanation: The 'wss://' protocol specifies WebSocket Secure, which uses encryption to protect data in transit, making it essential for sensitive applications like financial tickers. Option B, 'ws:/', is incorrect because it lacks security. Option C, 'https://', is appropriate for HTTP but not WebSocket communication. Option D, 'sock://', is not a recognized protocol identifier for WebSockets.