Skip to content

Commit

Permalink
Add: emitUpdateEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
chlehdwon committed May 30, 2023
1 parent d77856f commit 5c40faa
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/modules/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,35 @@ const emitChatEvent = async (io, chat) => {
}
};

const emitUpdateEvent = async (io, roomId, userId) => {
try {
// 방의 모든 사용자에게 socket 메세지 업데이트 이벤트를 발생시킵니다.
if (!io || !roomId || !userId) {
throw new IllegalArgumentsException();
}

const { name, part } = await roomModel.findById(roomId, "name part");

if (!name || !part) {
throw new IllegalArgumentsException();
}

const userIds = part.map((participant) => participant.user);
await Promise.all(
userIds.map(async (userId) =>
io.in(`user-${userId}`).emit("chat_update", {
roomId,
})
)
);

return true;
} catch (err) {
logger.error(err);
return false;
}
};

// https://socket.io/how-to/use-with-express-session 참고
const startSocketServer = (server) => {
const io = new Server(server, {
Expand Down Expand Up @@ -237,5 +266,6 @@ const startSocketServer = (server) => {
module.exports = {
transformChatsForRoom,
emitChatEvent,
emitUpdateEvent,
startSocketServer,
};

0 comments on commit 5c40faa

Please sign in to comment.