Skip to content

Commit

Permalink
feat(core/adapter): share the server_id for optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Totodore committed Dec 30, 2024
1 parent b0fa19a commit eb00526
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
12 changes: 12 additions & 0 deletions crates/socketioxide-core/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ pub struct BroadcastOptions {
pub except: SmallVec<[Room; 4]>,
/// The socket id of the sender.
pub sid: Option<Sid>,
/// The target server id can be used to optimize the broadcast.
/// More specifically when we use broadcasting to apply a single action on a remote socket.
/// We now the server_id of the remote socket, so we can send the action directly to the server.
pub server_id: Option<Sid>,
}
impl BroadcastOptions {
/// Add any flags to the options.
Expand All @@ -72,6 +76,14 @@ impl BroadcastOptions {
..Default::default()
}
}
/// Create a new broadcast options from a remote socket data.
pub fn new_remote(data: &RemoteSocketData) -> Self {
Self {
sid: Some(data.id),
server_id: Some(data.server_id),
..Default::default()
}
}
}

/// A trait for types that can be used as a room parameter.
Expand Down
9 changes: 8 additions & 1 deletion crates/socketioxide/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,12 @@ impl<A> RemoteSocket<A> {
}
}
/// Consume the [`RemoteSocket`] and return its underlying data
#[inline]
pub fn into_data(self) -> RemoteSocketData {
self.data
}
/// Get a ref to the underlying data of the socket
#[inline]
pub fn data(&self) -> &RemoteSocketData {
&self.data
}
Expand Down Expand Up @@ -192,34 +194,39 @@ impl<A: Adapter> RemoteSocket<A> {
/// # Get all room names this remote socket is connected to.
///
/// See [`Socket::rooms`] for more info.
#[inline]
pub async fn rooms(&self) -> Result<Vec<Room>, A::Error> {
self.adapter.rooms(self.get_opts()).await
}

/// # Add the remote socket to the specified room(s).
///
/// See [`Socket::join`] for more info.
#[inline]
pub async fn join(&self, rooms: impl RoomParam) -> Result<(), A::Error> {
self.adapter.add_sockets(self.get_opts(), rooms).await
}

/// # Remove the remote socket from the specified room(s).
///
/// See [`Socket::leave`] for more info.
#[inline]
pub async fn leave(&self, rooms: impl RoomParam) -> Result<(), A::Error> {
self.adapter.del_sockets(self.get_opts(), rooms).await
}

/// # Disconnect the remote socket from the current namespace,
///
/// See [`Socket::disconnect`] for more info.
#[inline]
pub async fn disconnect(self) -> Result<(), RemoteActionError> {
self.adapter.disconnect_socket(self.get_opts()).await?;
Ok(())
}

#[inline(always)]
fn get_opts(&self) -> BroadcastOptions {
BroadcastOptions::new(self.data.id)
BroadcastOptions::new_remote(&self.data)
}
}
impl<A> fmt::Debug for RemoteSocket<A> {
Expand Down

0 comments on commit eb00526

Please sign in to comment.