Saurabh
Saurabh

Reputation: 22873

Facebook like chat database schema in mongodb

I am planing a TCP/IP chat server in node.js and I want to include almost all the features of Facebook/gmail chat.

I have two ideas on the database schema -

First -

1) When user1 start chat with user2 and user3 I'll check in the database that are these users have chatted before (is there any room created for these users?) if not then I'll create a room for these users.

2) I'll save chat messages in messages collection and every chat msg in a separate document, I am thinking to save these details related to a chat msg msg sender_user room timestamp attachment

Issue with this approach -

1) The way I check if user1, user2 and user3 have chatted before (is there any room created for these before) required "$all" in query and this is slow and not much scalable (see my previous question - Search in Array in mongodb ). Is there any better approach on this search?

Second -

1) When user1 start chat with user2 and user3 I'll create a collection (soft of a room) for this chat and in this collection I'll save chat msgs in document

Issue with this approach -

1) How should I decide on the collection name? should I name it like "user1,user2,user3"? 2) How will I check if a collection (room) already created for user2, user3, user 1 because "user1,user2,user3" is not same as "user2,user3,user1" ?

I am also looking for more ideas because I am not fully satisfied with my approaches. How big giants (Facebook, Gmail) do their database schema for maximum performance and scalability?

Any help would me much appreciated.

Upvotes: 2

Views: 3127

Answers (1)

Leonel Machava
Leonel Machava

Reputation: 1531

The big giants (Facebook, Gmail, etc) did not design their chat systems from scratch. Their chat systems are built around the XMPP standard. If you follow this standard you could use an existing, robust and well tested chat server such as ejabberd or openfire (others here). For advanced applications these servers allow you to develop extensions (modules or components).

You would just have to write the client-side code. For web browsers you could use the Strophe library.

The book Professional XMPP Programming with JavaScript and jQuery has great examples of the application of the XMPP standard.

Upvotes: 3

Related Questions