DEV Community

Mohd Ahmad
Mohd Ahmad

Posted on

Make a chat app with Node JS

I want to make a chat app with react, node, socket.io, and Prisma. I am unable to add direct message (dm) functionality but added group functionality.

My Schema

datasource db {
  url      = env("DATABASE_URL")
  provider = "postgresql"
}

generator client {
  provider = "prisma-client-js"
}

model User {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  email     String   @unique
  username  String
  password  String
  sender    Messages @relation("sender")
  receiver  Messages @relation("receiver")
}

model Group {
  id        Int        @id @default(autoincrement())
  messages  Messages[]
  user      Users[]
}

model Messages {
  id          Int     @id @default(autoincrement())
  text        String 
  group       Group   @relation(fields: [groupId], references: [id])
  groupId     Int
  sender      User    @relation("sender", fields: [senderId], references: [id])
  senderId    Int
  reciever    User    @relation("reciever", fields: [recieverId], references: [id])
  recieverId  Int
}
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
bradstondev profile image
Bradston Henry

I don't have any experience with Prisma but you should be able to message a specific client in Socket using the Client's ID (which would be managed on your Node Server).

This is the socket documentation that talks about a client talking directly to a client: socket.io/docs/v4/server-socket-in...

Only think I would note is that is has become my practice to have a object that connects my Socket generated Client ID to the my custom created user IDs. That way I can always correlate the two when a client is connected to the server.

Collapse
 
ciochetta profile image
Luis Felipe Ciochetta

what kind of issues are you having?

have you tried to apply the same logic you use for group messages but have only two users in the room?

Collapse
 
mohdahmad1 profile image
Mohd Ahmad

I have done group messages, private messages, but stuck on a part on how to check user's friends or whome he chats with