» Rust: Make Web Chat App with Socket.IO » 2. Development » 2.4 Group Messages

Group Messages

Create Group

A Socket.IO room is a virtual concept that allows you to group sockets (connections) together. Sockets that are in the same room can communicate with each other easily. This feature is very useful when you want to broadcast messages to specific groups of clients, rather than broadcasting to all connected clients.

Changes in src/main.rs:

@@ -25,6 +25,13 @@ struct ChatData {
     msg: String,
 }
 
+#[derive(Serialize, Deserialize, Debug, Clone)]
+struct CreateGroupData {
+    sids: Vec<String>,
+    name: String,
+    id: String,
+}
+
 type UserStore = HashMap<String, User>;
 
 async fn on_connect(s: SocketRef) {
@@ -60,6 +67,20 @@ async fn on_connect(s: SocketRef) {
         let to = data.to.clone();
         s.to(to).emit("chat", data).ok();
     });
+
+    // Create Room
+    s.on(
+        "create-group",
+        |s: SocketRef, Data::<CreateGroupData>(data)| async move {
+            for socket_id in data.sids.clone() {
+                s.within(socket_id).join(data.id.clone()).ok();
+            }
+            let room_id = data.id.clone();
+            let room_name = data.name.clone();
+            s.within(data.id.clone()).emit("create-group", data).ok();
+            println!("Room {} => {} created", room_id, room_name)
+        },
+    );
 }
 
 #[tokio::main]
  • sids has all the socket IDs of users in a to-be-created group.
  • room_id is a unique alphanumeric string, which is more suitable than a user inputed room name here.

s.within(...).emit(...) notifies all the participants that a new group has been created.

Forward Group Messages

Changes in src/main.rs:

@@ -32,6 +32,13 @@ struct CreateGroupData {
     id: String,
 }
 
+#[derive(Serialize, Deserialize, Debug, Clone)]
+struct GroupChatData {
+    room: String,
+    speaker: String,
+    msg: String,
+}
+
 type UserStore = HashMap<String, User>;
 
 async fn on_connect(s: SocketRef) {
@@ -81,6 +88,14 @@ async fn on_connect(s: SocketRef) {
             println!("Room {} => {} created", room_id, room_name)
         },
     );
+
+    s.on(
+        "group-chat",
+        |s: SocketRef, Data::<GroupChatData>(data)| async move {
+            let room = data.room.clone();
+            s.to(room).emit("group-chat", data).ok();
+        },
+    );
 }
 
 #[tokio::main]

The to() method selects all clients in the given rooms except the current socket. So s.to(room).emit(...) forwards the messages to all the participants in the room except the sender.

PrevNext