How to manage websocket connection with actor model?

⚓ Rust    📅 2025-06-30    👤 surdeus    👁️ 5      

surdeus

Warning

This post was published 37 days ago. The information described in this article may have changed.

I'm trying to write a client/connector for some websocket based api. And I decide to implement it by build actors with tokio directly as decribed in this article: Actors with Tokio – Alice Ryhl.

The article specially mentioned how to handle a connection:

Multiple actors sharing a handle
Just like you can have multiple handles per actor, you can also have multiple actors per handle. The most common example of this is when handling a connection such as a TcpStream , where you commonly spawn two tasks: one for reading and one for writing. When using this pattern, you make the reading and writing tasks as simple as you can — their only job is to do IO. The reader task will just send any messages it receives to some other task, typically another actor, and the writer task will just forward any messages it receives to the connection.

I have several questions about it:

  1. Is the article suggesting that the reading actor and the writing actor share a common handle, like ConnHandle?
  2. The writing actor is just a regular actor, the corresponding handle should have some method like send(message) to send message to it via internal channel. But the reading actor is a bit different - unlike typical actors that you send message to, it send messages to you, or you receive messages from it instead. So the question is how to design the api for it?
    The article seems to suggest let the reading actor hold another actor's handle so it can send message out directly. But usually the "another actor" is some high level business logic actor, if the low-level connection actor holds a reference to the high-level business actor, it introduces a circular dependency that prevents you from constructing both actors in the first place. How to resolve this issue?

1 post - 1 participant

Read full topic

🏷️ rust_feed