Skip to content

Commit

Permalink
refactor(chat-app): add utility function to emit socket event inside …
Browse files Browse the repository at this point in the history
…the controllers
  • Loading branch information
wajeshubham committed Aug 17, 2023
1 parent 92cfab5 commit 7277197
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 34 deletions.
2 changes: 2 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,5 @@ export const ChatEventEnum = Object.freeze({
// ? when participant starts typing
TYPING_EVENT: "typing",
});

export const AvailableChatEvents = Object.values(ChatEventEnum);
61 changes: 33 additions & 28 deletions src/controllers/apps/chat-app/chat.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ChatEventEnum } from "../../../constants.js";
import { User } from "../../../models/apps/auth/user.models.js";
import { Chat } from "../../../models/apps/chat-app/chat.models.js";
import { ChatMessage } from "../../../models/apps/chat-app/message.models.js";
import { emitSocketEvent } from "../../../socket/index.js";
import { ApiError } from "../../../utils/ApiError.js";
import { ApiResponse } from "../../../utils/ApiResponse.js";
import { asyncHandler } from "../../../utils/asyncHandler.js";
Expand Down Expand Up @@ -197,10 +198,12 @@ const createOrGetAOneOnOneChat = asyncHandler(async (req, res) => {
if (participant._id.toString() === req.user._id.toString()) return; // don't emit the event for the logged in use as he is the one who is initiating the chat

// emit event to other participants with new chat as a payload
req.app
.get("io")
.in(participant._id.toString())
.emit(ChatEventEnum.NEW_CHAT_EVENT, payload);
emitSocketEvent(
req,
participant._id?.toString(),
ChatEventEnum.NEW_CHAT_EVENT,
payload
);
});

return res
Expand Down Expand Up @@ -258,10 +261,12 @@ const createAGroupChat = asyncHandler(async (req, res) => {
payload?.participants?.forEach((participant) => {
if (participant._id.toString() === req.user._id.toString()) return; // don't emit the event for the logged in use as he is the one who is initiating the chat
// emit event to other participants with new chat as a payload
req.app
.get("io")
.in(participant._id.toString())
.emit(ChatEventEnum.NEW_CHAT_EVENT, payload);
emitSocketEvent(
req,
participant._id?.toString(),
ChatEventEnum.NEW_CHAT_EVENT,
payload
);
});

return res
Expand Down Expand Up @@ -339,10 +344,12 @@ const renameGroupChat = asyncHandler(async (req, res) => {
// logic to emit socket event about the updated chat name to the participants
payload?.participants?.forEach((participant) => {
// emit event to all the participants with updated chat as a payload
req.app
.get("io")
.in(participant._id.toString())
.emit(ChatEventEnum.UPDATE_GROUP_NAME_EVENT, payload);
emitSocketEvent(
req,
participant._id?.toString(),
ChatEventEnum.UPDATE_GROUP_NAME_EVENT,
payload
);
});

return res
Expand Down Expand Up @@ -385,10 +392,12 @@ const deleteGroupChat = asyncHandler(async (req, res) => {
chat?.participants?.forEach((participant) => {
if (participant._id.toString() === req.user._id.toString()) return; // don't emit the event for the logged in use as he is the one who is deleting
// emit event to other participants with left chat as a payload
req.app
.get("io")
.in(participant._id.toString())
.emit(ChatEventEnum.LEAVE_CHAT_EVENT, chat);
emitSocketEvent(
req,
participant._id?.toString(),
ChatEventEnum.LEAVE_CHAT_EVENT,
chat
);
});

return res
Expand Down Expand Up @@ -424,10 +433,12 @@ const deleteOneOnOneChat = asyncHandler(async (req, res) => {
);

// emit event to other participant with left chat as a payload
req.app
.get("io")
.in(otherParticipant?._id.toString())
.emit(ChatEventEnum.LEAVE_CHAT_EVENT, payload);
emitSocketEvent(
req,
otherParticipant._id?.toString(),
ChatEventEnum.LEAVE_CHAT_EVENT,
payload
);

return res
.status(200)
Expand Down Expand Up @@ -485,10 +496,7 @@ const addNewParticipantInGroupChat = asyncHandler(async (req, res) => {
}

// emit new chat event to the added participant
req.app
.get("io")
.in(participantId)
.emit(ChatEventEnum.NEW_CHAT_EVENT, payload);
emitSocketEvent(req, participantId, ChatEventEnum.NEW_CHAT_EVENT, payload);

return res
.status(200)
Expand Down Expand Up @@ -546,10 +554,7 @@ const removeParticipantFromGroupChat = asyncHandler(async (req, res) => {
}

// emit leave chat event to the removed participant
req.app
.get("io")
.in(participantId)
.emit(ChatEventEnum.LEAVE_CHAT_EVENT, payload);
emitSocketEvent(req, participantId, ChatEventEnum.LEAVE_CHAT_EVENT, payload);

return res
.status(200)
Expand Down
11 changes: 7 additions & 4 deletions src/controllers/apps/chat-app/message.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import mongoose from "mongoose";
import { ChatEventEnum } from "../../../constants.js";
import { Chat } from "../../../models/apps/chat-app/chat.models.js";
import { ChatMessage } from "../../../models/apps/chat-app/message.models.js";
import { emitSocketEvent } from "../../../socket/index.js";
import { ApiError } from "../../../utils/ApiError.js";
import { ApiResponse } from "../../../utils/ApiResponse.js";
import { asyncHandler } from "../../../utils/asyncHandler.js";
Expand Down Expand Up @@ -136,10 +137,12 @@ const sendMessage = asyncHandler(async (req, res) => {
if (participantObjectId.toString() === req.user._id.toString()) return;

// emit the receive message event to the other participants with received message as the payload
req.app
.get("io")
.in(participantObjectId.toString())
.emit(ChatEventEnum.MESSAGE_RECEIVED_EVENT, receivedMessage);
emitSocketEvent(
req,
participantObjectId.toString(),
ChatEventEnum.MESSAGE_RECEIVED_EVENT,
receivedMessage
);
});

return res
Expand Down
16 changes: 14 additions & 2 deletions src/socket/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import cookie from "cookie";
import jwt from "jsonwebtoken";
import { Server, Socket } from "socket.io";
import { ChatEventEnum } from "../constants.js";
import { AvailableChatEvents, ChatEventEnum } from "../constants.js";
import { User } from "../models/apps/auth/user.models.js";
import { ApiError } from "../utils/ApiError.js";

Expand Down Expand Up @@ -100,4 +100,16 @@ const initializeSocketIO = (io) => {
});
};

export { initializeSocketIO };
/**
*
* @param {import("express").Request} req - Request object to access the `io` instance set at the entry point
* @param {string} roomId - Room where the event should be emitted
* @param {AvailableChatEvents[0]} event - Event that should be emitted
* @param {any} payload - Data that should be sent when emitting the event
* @description Utility function responsible to abstract the logic of socket emission via the io instance
*/
const emitSocketEvent = (req, roomId, event, payload) => {
req.app.get("io").in(roomId).emit(event, payload);
};

export { initializeSocketIO, emitSocketEvent };

0 comments on commit 7277197

Please sign in to comment.