pub struct RedisRoomRegistry<C = MultiplexedConnection> { /* private fields */ }redis only.Expand description
Multi-node crate::room::RoomRegistry: local RoomManager + Redis
pub/sub fan-out. See the module docs for topology and delivery
semantics.
The generic parameter is the publish connection (redis’
ConnectionLike); it defaults to the production
MultiplexedConnection and exists so tests can substitute a mock —
in application code always write the default RedisRoomRegistry.
Implementations§
Source§impl RedisRoomRegistry<MultiplexedConnection>
impl RedisRoomRegistry<MultiplexedConnection>
Sourcepub async fn connect(url: &str) -> Result<Self, WsError>
pub async fn connect(url: &str) -> Result<Self, WsError>
Connect with the default channel prefix
(DEFAULT_CHANNEL_PREFIX).
§Errors
WsError::Redis when the URL is invalid or the server is
unreachable.
Source§impl<C> RedisRoomRegistry<C>
impl<C> RedisRoomRegistry<C>
Sourcepub fn from_parts(
local: RoomManager,
client: Client,
publish: C,
prefix: impl Into<String>,
) -> Self
pub fn from_parts( local: RoomManager, client: Client, publish: C, prefix: impl Into<String>, ) -> Self
Assemble a registry from a local manager, a Redis client (used by
Self::run_subscriber), and an existing publish connection.
Public to let integrators and tests supply custom
ConnectionLike implementations (the trait-mocked publish path);
prefer Self::connect in application code.
Source§impl<C> RedisRoomRegistry<C>
impl<C> RedisRoomRegistry<C>
Sourcepub fn local(&self) -> &RoomManager
pub fn local(&self) -> &RoomManager
The local half of the registry — the ordinary RoomManager API
for joins, leaves, local subscription, and cleanup.
Sourcepub fn channel_name(&self, room_id: &str) -> String
pub fn channel_name(&self, room_id: &str) -> String
Channel name for a room id.
Sourcepub fn room_id_from_channel<'a>(&self, channel: &'a str) -> Option<&'a str>
pub fn room_id_from_channel<'a>(&self, channel: &'a str) -> Option<&'a str>
Room id carried by a channel name, if it matches the prefix.
Source§impl<C> RedisRoomRegistry<C>
impl<C> RedisRoomRegistry<C>
Sourcepub async fn broadcast(
&self,
room_id: &str,
frame: impl Into<Frame>,
) -> Result<usize, WsError>
pub async fn broadcast( &self, room_id: &str, frame: impl Into<Frame>, ) -> Result<usize, WsError>
Broadcast a frame to a room across the fleet.
- Fans out to the local room’s receivers (creating the room if
this node does not host it yet — matching
RoomRegistry::get_or_createsemantics). PUBLISHes the envelope toprefix + room_idfor every other node.
Returns the number of local receivers reached. An empty local
room is not an error here (unlike crate::room::Room::broadcast):
other nodes may still hold receivers, so Ok(0) just means “no local
subscriber”. A Redis failure surfaces as WsError::Redis.
At-most-once: see the module docs — delivery on other nodes is best-effort.
Sourcepub async fn publish(&self, frame: &Frame, room_id: &str) -> Result<(), WsError>
pub async fn publish(&self, frame: &Frame, room_id: &str) -> Result<(), WsError>
Publish one frame to room_id’s channel (no local delivery).
Sourcepub fn apply_remote(
&self,
room_id: &str,
envelope: &[u8],
) -> Result<Option<usize>, WsError>
pub fn apply_remote( &self, room_id: &str, envelope: &[u8], ) -> Result<Option<usize>, WsError>
Apply a raw pub/sub envelope to the local registry: decode it, drop self-echoes, and deliver to the local room only if this node hosts it (remote traffic never materializes rooms).
Returns Ok(Some(n)) with the local receiver count when delivered,
Ok(None) when the message was a self-echo or for an unhosted room,
and the decode error for malformed envelopes.
Source§impl RedisRoomRegistry<MultiplexedConnection>
impl RedisRoomRegistry<MultiplexedConnection>
Sourcepub async fn run_subscriber(&self) -> Result<(), WsError>
pub async fn run_subscriber(&self) -> Result<(), WsError>
Run the subscriber loop on the current task: PSUBSCRIBE prefix* and apply every message to the local registry until the
Redis connection dies.
Spawn it once per node at startup (see the module example); to survive Redis restarts wrap it in your own retry/backoff — the loop intentionally returns instead of hiding reconnect policy.
§Errors
WsError::Redis on subscription/transport failure. Malformed
envelopes from the channel are dropped (fail-closed), not fatal.