Actix Websockets with Protobuf

Ferdinand de Antoni
6 min readOct 27, 2019

--

Actix is a great library with many helpful modules for building RESTful applications with, for example, websockets. Actix has many good examples, including one for building an Actix based websockets echo server, as well as a client. Here we will create a simple Actix Websocket project using the Prost protocol buffers library for communicating over the websocket.

(What we will do is combine the Actix websocket example with the Prost example.)

Project Setup

The first thing we do is create a new cargo project:

Go into the project and open up the new Cargo.toml file. Add the following to it:

In this project we will create two binaries: server and client. The server will be a simple websocket echo server which will echo whatever it will receive back to the client. You can copy the server code from the Actix websocket example almost verbatim. From the Actix example websocket project, copy over the examples/websocket/static folder and place it in your project. Copy over the contents from the example’s examples/websocket/src/main.rs and place it in a new file called src/server.rs in your project.

Prost Setup

Now that the server part is complete, we can start building our client. First we set up Prost. You will have noticed in the Cargo.toml that we use prost-build and prost itself. The prost-build dependency takes care of compiling our proto files into Rust code. Let’s create a proto file first containing our probobuf message. Create a file called src/items.proto with the following contents:

Now that we have our proto file, we will need to tell Cargo to compile it. For this we will need to create a build.rs file in the root of the project with the following contents:

This instructs the prost-build library to compile our src/items.proto into rust code when we do the build.

Models Module

With Prost set up properly and our proto file in place, let’s create a models module containing our Shirt model. Create src/models.rs with the following contents:

Next, let’s implement some functions to create a Shirt, serialize it, and then deserialize it. In the same file, add the following:

To test our code out, we can add some unit tests in the same file:

With our models module ready, we can add it to the src/main.rs file by adding the following to the top of it:

We can check everything is working by running the unit tests:

Client Module

With our models module ready, we can start creating our websocket client to communicate with the echo server. Open up the src/main.rs file again and edit the top part so that it looks as follows:

Now create a file called src/client.rs. With this we are creating a client module. Add the following contents to this file:

We created our WsClient struct and implemented an Actix Actor on it with a basic heartbeat. Next we will add the implementation that will receive the shirt colour and send it over to the websocket server:

We define a message struct that will contain the shirt colour which will be sent to our WsClient actor. We implement a method that takes this message and sends the new shirt as a binary message to the echo websocket server.

Next we implement a stream handler for receiving messages from the websocket server:

If we receive a text message from the websocket server, we just show that on console. If we receive a binary message we assume it is a shirt and try to deserialize it as such.

Lastly we implement a write handler for the websocket sink on our WsClient:

Main Method

Our modules are now done, we can implement the main method that will launch our websocket client, connect to the echo websocket server, and send a shirt to it. Add the following to the src/main.rs file:

With everything now in place, we can start the server first:

The server should now be running. In another console, start the client:

The client should now be running as well. Type in a shirt colour to send a shirt, and receive it back from the server, e.g.:

This test project is also on github: actix-ws-prost.

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Responses (1)

Write a response