Skip to content

Commit

Permalink
Fixed a bug with user status change in both socket-io-server and the …
Browse files Browse the repository at this point in the history
…chat page
  • Loading branch information
PezhmanGhavami committed Jun 1, 2023
1 parent 2784a0a commit 0bd7598
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
11 changes: 9 additions & 2 deletions client/src/pages/chat/chat.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,17 @@ function Chat() {
// Recipient status change
socket.on(
`chat-${params.chatId}-recipient-status-change`,
({ isOnline }) => {
({ isOnline, lastOnline }) => {
const payload: {
isOnline: boolean;
lastOnline?: Date;
} = { isOnline };

if (lastOnline) payload.lastOnline = lastOnline;

setCurrentRecipientUser((prev) => ({
...prev!,
isOnline,
...payload,
}));
},
);
Expand Down
5 changes: 3 additions & 2 deletions server/socket.io-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,8 +686,9 @@ const startSocketServer = (
socket.on("disconnect", async (reason) => {
try {
const activeSockets = io.sockets.adapter.rooms.get(id as string);
const lastOnline = new Date(Date.now());

if (activeSockets?.size) {
const lastOnline = new Date(Date.now());
await prisma.session.update({
where: {
id: updatedSession.id,
Expand All @@ -709,10 +710,10 @@ const startSocketServer = (
.to(chat.id)
.emit(`chat-${chat.id}-recipient-status-change`, {
isOnline: false,
lastOnline,
});
}
}
const lastOnline = new Date(Date.now());
await prisma.session.update({
where: {
id: updatedSession.id,
Expand Down

0 comments on commit 0bd7598

Please sign in to comment.