In this exercise you will learn how to

  • use a web server with actix & Actors

  • use websockets to communicate messages between frontend and web server

  • use the env_logger crate

  • implement custom message handler(s)

The web server runs at http://localhost:8080. Once started, open the link in a browser. The rudimentary UI allows you to connect to the web server, meaning you will join the chat server. Once connected you can write messages in the text input field. Received messages will be displayed in the text box.

Tasks

  1. Broadcast the client message to all other connected clients

    • to see the effect connect at least twice in separate tabs

  2. Implement a custom message to disconnect from the chat server

    • you should not receive new messages from other clients thereafter

    • notify other clients of disconnect

  3. Check that you do not receive your own chat message back

  4. Add heartbeat logic to remove a client from the chat server after 10s of inactivity (e.g. tab closed)

Getting Started

Clone the repository at ferrous-systems/teaching-material. You find the assignment in folder assignments/actix/chat-websockets.

Run the web server with cargo run to start the chat server.

The given dependencies of the Cargo.toml are not the most recent versions due to some incompatibilities. Updating these may not compile the program anymore.

Help

Further documentation

Bonus Tasks

  1. Add support to enter & display the client name

    • For this you need to adapt the index.html page, remove the visibility:hidden; style from the name input field

    • Think about how to distinguish setting the name from sending a text, e.g. using a prefix

    • Expand the WsChatSession struct to store the name, adapt code accordingly

    • If the name is available output it instead of displaying the text "Someone" in code

  2. What issues might the chat server have?

    • What is not working as you would expect?