Browser and Web APIs
Exercise: WebSockets
The backend chat service provides WebSocket connections at
wss://chat.distsys.ch/ws.
-
Replace the
renderMessages function with a renderMessage function that renders a single message, and update the fetchMessages function accordingly.
-
Remove the chat message polling and instead create a web socket that connects to the chat service.
-
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.
-
In the
postMessage function, send the new chat message using the web socket.