This tutorial will walk you through each step of writing a blazingly fast WebSocket client in Actix Web, in-depth, and with a working repository as reference.

We’ll be building a simple chatroom that echos messages to everyone in a room, and include private messages. I’ll also explain every step, so you can extend this example and write your own WebSocket server in Actix Web.

Repo for the completed project: https://github.com/antholeole/actix-sockets

Prerequisites:

  1. Know what WebSockets are at a general level
  2. Know some basic Rust

Everything else will be talked about in this tutorial.

Warm up to the Actix Architecture

In the Actix architecture, there are two primary components: Actors and Messages. Think of each actor as its own object in memory, with a mailbox. Actors can read their mailbox and respond to their mail accordingly, whether it be by sending mail to another actor, changing its state, or maybe by doing nothing at all. That’s it! That’s all that actors are — simple little things that read and respond to mail.

Actors are so ungodly fast because they work entirely independent of each other. One actor can be on its own thread, or on a different machine entirely. As long as the actor can read its mail, it works perfectly.

It’s important to note that the actor just exists in memory, with its address passed around like Addr<Actor>. The Actor itself can mutate its properties (maybe you have a “messages_received” property, and you need to increment it on every message) but you can’t do that anywhere else. Instead, with the Addr<Actor> element, you can do .send(some_message) to put a message in that Actor’s mailbox.

In Actix web, each socket connection is its own Actor, and the “Lobby” (we’ll get to that) is its own actor also.

#rust #actors #actix-web

WebSockets in Actix Web Full Tutorial — WebSockets & Actors
19.95 GEEK