4

I am making a chat app for android with phone gap and PHP at back end. as its my first time am building a chat app,so i m little confused that how to store chat messages,

  1. storing messages in database

    A. one row for each message (but with this size of db gonna large and speed slow)

    B. one row for all messages but this is not good i think

  2. storing messages in text file and appending but i feel its gonna be very slow and hard to maintain

  3. storing messages in XML but time in parsing XML and storing complexity gonna create problem

so i end up with no solution,please suggest me some solution and i m new to forum so be patient at my silly mistakes and how big fishes like fb,whats app store their messages>

user3175986
  • 41
  • 1
  • 3
  • Possible duplicate of http://stackoverflow.com/questions/6314665/file-vs-database-for-storage-efficiency-in-chat-app – Don Cruickshank Mar 10 '14 at 10:27
  • @DonCruickshank that question was asked two years ago ,so may be some new ,better and fast solution came into existence til now – user3175986 Mar 10 '14 at 10:30

2 Answers2

2

Of the three options for this I would go with your first option (part a) assuming you meant a relational DB (like mysql). The size of the db will get large if you keep everything. However. Do you need to keep everything? One option it to prune old messages periodically.

My preferred option would actually be a nosql document DB for this (something like mongo) as you probably won't need to model any complex relational data. I would then model each "chat" as a document. Each chat would have an array of messages. This way any time a new message comes in you push it on to the messages array for the relevant chat. I would also consider archiving older messages in the array if I expected the chats to persist for a long time or generate a lot of data.

After I had done this if speed was still an issue I would look at adding some in memory caching (memcached or apcu or both). All messages would be posted and retrieved from the cache so any popular chats are going to stay in memory giving you a nice speed boost.

How far you go down this path depends on your needs.

Steve B
  • 634
  • 4
  • 11
  • u may be familar with whats app,if my app is something more like it then what will be ur suggestion ( it contain peer to peer chats and group chat also) and yes i needs old chat but may be user not gonna frequently used them – user3175986 Mar 10 '14 at 11:06
  • Well my first thought for something like that using mongodb would be one document for each "chat" (i.e. one group or 1 peer to peer). The chat document would have then have an array of what's been said. Every new message received gets pushed on to that array. – Steve B Mar 10 '14 at 11:22
  • can u provide me link where little details is available about your solution ,it would be great if u can – user3175986 Mar 11 '14 at 05:44
0

I find third option woul be better.its gonna be better than all other application.try to store on client's device as xml itself.so that u could free your database

uksp
  • 27
  • 8