Browser and Web APIs

Exercise: WebSockets

The backend chat service provides WebSocket connections at wss://chat.distsys.ch/ws.
  1. Replace the renderMessages function with a renderMessage function that renders a single message, and update the fetchMessages function accordingly.
  2. Remove the chat message polling and instead create a web socket that connects to the chat service.
  3. Implement a receiveMessages function that registers the following event handlers on the web socket:
    • onopen sends the authentication token as the first message
    • onmessage renders the received chat message
    Call the function after the existing messages have been fetched.
  4. In the postMessage function, send the new chat message using the web socket.