Skip to content

Commit

Permalink
clientId => socketId
Browse files Browse the repository at this point in the history
  • Loading branch information
barnabasmolnar committed Dec 13, 2023
1 parent 511f784 commit 7669f11
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import express from "express";
import http from "http";
import { Server as SocketIO } from "socket.io";

type UserToFollow = { clientId: string; username: string };
type UserToFollow = {
socketId: string;
userId: string | null;
username: string;
};
type OnUserFollowedPayload = {
userToFollow: UserToFollow;
action: "follow" | "unfollow";
Expand Down Expand Up @@ -85,7 +89,7 @@ try {
);

socket.on("on-user-follow", async (payload: OnUserFollowedPayload) => {
const roomID = `follow_${payload.userToFollow.clientId}`;
const roomID = `follow_${payload.userToFollow.socketId}`;
switch (payload.action) {
case "follow":
await socket.join(roomID);
Expand All @@ -96,7 +100,7 @@ try {
// Notify the user to follow that someone has followed them
// More precisely, send the list of users that are following them
// so that they can make decisions based on that
io.to(payload.userToFollow.clientId).emit(
io.to(payload.userToFollow.socketId).emit(
"follow-room-user-change",
followedBy,
);
Expand All @@ -107,7 +111,7 @@ try {
const _sockets = await io.in(roomID).fetchSockets();
const _followedBy = _sockets.map((socket) => socket.id);

io.to(payload.userToFollow.clientId).emit(
io.to(payload.userToFollow.socketId).emit(
"follow-room-user-change",
_followedBy,
);
Expand All @@ -133,8 +137,8 @@ try {
}

if (isFollowRoom && otherClients.length === 0) {
const clientId = roomID.replace("follow_", "");
io.to(clientId).emit("broadcast-unfollow");
const socketId = roomID.replace("follow_", "");
io.to(socketId).emit("broadcast-unfollow");
}
}
});
Expand Down

0 comments on commit 7669f11

Please sign in to comment.