You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am making a simple chat app with React and Socket Io(NodeJS) but currently, I fetch data from a database inside of socket event listeners about 2 or 3 times in an event listener. (e.g To set previous messages in a chat room etc)
socket.on('join_room', async (data) => {
const chat = awiat prisma.chat.findUnique({
where: {
name: data.roomName,
},
include: {
messages: {
include: {
sender: true,
},
},
users: true,
},
})
const messages = chat.messages;
const users = chat?.users.map((user) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
delete user.password;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
delete user.createdAt;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
delete user.updatedAt;
return user;
});
socket.emit('setMessagesAndMemebers', { messages, users })
// Check if a user joined the room for the first time.
const userInChat = await prisma.chat.findFirst({
where: {
AND: [
{
name: data.roomName,
},
{
users: {
some: {
username: data.username,
},
},
},
],
},
});
// A new member to the room.
if (!userInChat) {
// Connect a user to the existing chat.
await prisma.chat.update({
where: {
name: data.roomName,
},
data: {
users: {
connect: {
username: data.username,
},
},
},
});
const newUser = await prisma.user.findUnique({
where: {
username: data.username,
},
});
if (newUser) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
delete newUser.password;
io.to(data.roomName).emit('enter_new_member', { newUser });
})
}
However, it looks like it is a lot of work to do for a socket. I think communication between sockets should be as fast as possible.
My thought possibly be wrong so I would appreciate any thoughts about this problem.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am making a simple chat app with React and Socket Io(NodeJS) but currently, I fetch data from a database inside of socket event listeners about 2 or 3 times in an event listener. (e.g To set previous messages in a chat room etc)
However, it looks like it is a lot of work to do for a socket. I think communication between sockets should be as fast as possible.
My thought possibly be wrong so I would appreciate any thoughts about this problem.
Beta Was this translation helpful? Give feedback.
All reactions