05 / 05

List the properties and methods available on the websocket object.

Difficulty: 2/10
WebSocket API, Event handling, Connection lifecycle
Properties:
  1. 1

    WebSocket.binaryType: The binary data type used by the connection.

  2. 2

    WebSocket.bufferedAmount: The number of bytes of queued data.

  3. 3

    WebSocket.extensions: The extensions selected by the server.

  4. 4

    WebSocket.protocol: The sub-protocol selected by the server.

  5. 5

    WebSocket.readyState: The current state of the connection.

  6. 6

    WebSocket.url: The absolute URL of the WebSocket.

methods
  1. 1

    send(data): Sends data to the server over the WebSocket connection.

  2. 2

    close([code, reason])Closes the WebSocket connection. You can optionally provide a status code and a reason for closing.

  3. 3

    addEventListener(type, listener): Adds an event listener for a specific event type

  4. 4

    removeEventListener(type, listener): Removes an event listener.

Scenario Questions

0-2 years experience

  1. 1You're building a chat feature. The WebSocket connection just opened — how do you send a JSON message to the server?
  2. 2What happens if you call socket.send() immediately after new WebSocket() but before the 'open' event fires?
  3. 3The server sends you a message. Where do you write the code to parse and display it?

2-5 years experience

  1. 1Users report messages occasionally not arriving. You see 'onerror' firing but 'onclose' doesn't. How do you debug whether it's a network blip or server-side drop?
  2. 2You need to support both JSON and binary (protobuf) payloads on the same connection. How do you structure the message handling so the rest of your app doesn't care about the wire format?
  3. 3The backend team wants to add a heartbeat. Where do you put the ping/pong logic — client, server, or both — and how do you avoid false disconnects during GC pauses?

5-8 years experience

  1. 1At 50k concurrent connections, you're seeing memory growth from unclosed sockets. Walk me through how you'd instrument the WebSocket lifecycle to find leaks — what events, what metrics, what cleanup?
  2. 2Design a backpressure strategy: the client produces messages faster than the network can drain. Do you buffer, drop, or apply flow control? How does that interact with the WebSocket's internal send buffer?
  3. 3You're migrating from long-polling to WebSockets for a real-time dashboard. The legacy code expects request/response semantics. How do you bridge the paradigm gap without rewriting all consumers?

8+ years experience

  1. 1Three teams own services that push real-time updates to a shared WebSocket gateway. How do you define a protocol contract (frame format, auth, versioning) that lets teams deploy independently without breaking clients?
  2. 2Your company acquired a product that uses Socket.IO. You're standardizing on raw WebSockets. Plan the migration: compatibility layer, feature parity checklist, rollback criteria, and how you measure 'done'.
  3. 3The security team mandates mutual TLS for all WebSocket connections. Your load balancer terminates TLS. How do you propagate client cert identity to the WebSocket application layer without redesigning the auth system?

Follow-up Questions

  • How do you handle binary vs text frames differently?
  • What's your strategy for detecting dead connections?
  • How would you implement automatic reconnection with backoff?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.