1

I am creating a system in which I am planning to let User A generate a special chat link so he/she can communicate with User B. This is a 1-1 chat model. The schema of my model is below. I need to know if its practical or if it can be improved.

table messages
chatid; {fk}
message_id; {pk}
timestamp;
sender;

table chat
chatid; {pk}
userA;
userB;
timestamp;

Here is the control flow of the scenario:

  1. User A has intentions to chat with User B.
  2. User A goes into the generate chat session page.
  3. User A selects the User B from the list.
  4. User A generates the chat session /app/chatid/007
  5. User A copies and pastes the URL to sent it to User B via the inbox.
  6. User A and User B agree on a time they wish to chat

I know, a bit weird, because I dont know how to implement a system where user is checked for online/offline, although you could pop in if you have ideas.

  1. User A opens the chat URL on hh;mm and so does User B.
  2. They start chatting.
  3. The page doesn't reload and they chat with fun.

The end. Waiting for your responses.

Editted by Asker

I was looking at this schema. I don't know how I would further add to it to show user a/user b being online/offline; perhaps last_activity? You tell.

tbl_chat
-------------
chat_id (pk)
user_id_a (fk)
user_id_b (fk)
timestamp
tinyint;

tbl_msg
-------------
message_id (pk)
chat_id (fk)
user_id_sender (fk)
message_body
timestamp

added tinyint; to tbl_chat so when user A clicks kill chat, it makes it 1 , default of tinyint =0; So that kills chat.

Ali Gajani
  • 14,762
  • 12
  • 59
  • 100

1 Answers1

3

For the sake of being able to extend the chat to more than two users and not having to check both chat.userA and chat.userB in your queries I would not use userA and userB in the chat table. Rather I would add a table (e.g. chat_users) which has usernames/ids (whatever you put in userA and userB) and a chatid. Apart from that I would say that messages need a body.

table messages
chatid; {fk}
message_id; {pk}
timestamp;
userid; {fk}
body;

table chat
chatid; {pk}
timestamp;

table chat_user
userid; {fk}
chatid; {fk}
joidegn
  • 1,078
  • 9
  • 19