0

I'm trying to build a message/chat system between users.

Following rules are main features of message system.

  • Each user can send message to others and only one-to-one chat is accepted.(which means only two users can share messages). If there are users named A to Z, user A can send message to B to Z but all message are independent so it's like one to one chatting system. A, B and C can't have conversation in the same message box (chatting room)).
  • Message should be sent/received in realtime which is why I decided to use Socket.io
  • After user logged out, message history is saved so user can see it whenever he/she login again.

This is how I think it's going to work.

  • When user A login, server creates an unique room for user A by using unique identifier which is userId in req Token. So, every user join a room when they login.

    io.sockets.on('connection', function (socket) { socket.on('join', function (data) { socket.join(data.userId); }); });

    Now, other users can send message to user A in realtime. Of course user A can also send message to particular user and get msg in realtime.
    io.sockets.in('userId').emit('msg', {msg: 'hi!'});

  • If user A want to send message to user B but, user B is currently not logged-in which means there's no room of user B, then the message sent by user A is saved in .txt file and filepath is saved in database.

  • Later when user B checks the message box, user B can see all message history by loading .txt file.

So, it can be realtime chatting if both user A and B is logged-in and also can be message system if one user is not logged-in.

halfer
  • 19,824
  • 17
  • 99
  • 186
KimCrab
  • 2,231
  • 4
  • 15
  • 20
  • Why not save the chat to DB instead of text file? – AEonAX Aug 16 '15 at 05:27
  • @AEonAX I'm using MySQL and I haven't seen good practice. Many people recommend me to do it with txt file. If you recommend DB instead of text file, would you please tell me reasons why DB is better than txt file? I'm really not used to it. – KimCrab Aug 16 '15 at 05:33
  • Since you are going to provide Only 1 -1 Chat. It seems you would only need only this columns in your DB=> 1. MsgID 2. SenderId 3. ReceiverID 4.Message 5.IsDelivered. As well as another table to store users. Then You can Query by ReceiverID and IsDelivered fields – AEonAX Aug 16 '15 at 05:36
  • 1
    File based approach on server is difficult for maintaining consistency and integrity. – AEonAX Aug 16 '15 at 05:42

1 Answers1

0

Using Socket.io is good start, but it's gonna be a lots of work to build a comprehensive chat app. If you want to skip the pain building everything from scratch, you can refer to Hyphenate SDK (iOS, Android, and Web), which is real time communication over socket.

Hyphenate supports both Mobile and Web SDK with rich features and very reliable services. It also provides open source UI components so you don't need to build everything from scratch.

https://docs.hyphenate.io

https://github.com/HyphenateInc

Jerry
  • 1,018
  • 4
  • 13
  • 22