Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store real-time chat messages in database?

I am using mysqldb for my database currently, and I need to integrate a messaging feature that is in real-time. The chat demo that Tornado provides does not implement a database, (whereas the blog does.)

This messaging service also will also double as an email in the future (like how Facebook's message service works. The chat platform is also email.) Regardless, I would like to make sure that my current, first chat version will be able to be expanded to function as email, and overall, I need to store messages in a database.

Is something like this as simple as: for every chat message sent, query the database and display the message on the users' screen. Or, is this method prone to suffer from high server load and poor optimization? How exactly should I structure the "infrastructure" to make this work?

(I apologize for some of the inherent subjectivity in this question; however, I prefer to "measure twice, code once.")

Input, examples, and resources appreciated.
Regards.

like image 784
Friendly King Avatar asked Mar 15 '13 21:03

Friendly King


People also ask

Which database is good for storing chat messages?

We could certainly use a document store for our chat server, but due to the way our data will be stored, a SQL database matches our needs best. Among SQL databases, we have a number of options: PostgreSQL, MySQL and SQL Server are the most common server variants of SQL, while SQLite is the most common embedded variant.

What is the best database for a realtime chat server?

For storing your data i suggest you use MongoDB or MYSQL. If you use Node JS as backend engine then use MongoDB. Because when you combine Node JS with MongoDB your data exchange process will be very efficient. If you use others as a backend engine then use MYSQL or other RDMS.

How is chat data stored?

The chat history as well as the media files, are stored in the Microsoft server, not saved in OneDrive/SharePoint though since it requires you to export the chat history. If you ever have exported chat history on Skype then MS Team free personal life is similar to Skype in terms of chat history.

Are messages saved in a database?

Before you try recovering or saving your Android text messages, the first thing you should know is where text messages are stored on your phone. In general, Android SMS are stored in a database in the data folder located in the internal memory of the Android phone.


1 Answers

Tornado is a single threaded non blocking server.

What this means is that if you make any blocking calls on the main thread you will eventually kill performance. You might not notice this at first because each database call might only block for 20ms. But once you are making more than 200 database calls per seconds your application will effectively be locked up.

However that's quite a few DB calls. In your case that would be 200 people hitting send on their chat message in the same second.

What you probably want to do is use a queue with a non blocking API. So Tornado receives a chat message. You put it on the queue to be saved to the database by another process, then you send the chat message back out to the other chat members.

When someone connects to a chat session you also need to send off a request to the queue for all the previous messages, when the queue responds you send those off to the newly connected user.

That's how I would approach the problem anyway.

Also see this question and answer: Any suggestion for using non-blocking MySQL api on Tornado in Python3?

Just remember, Tornado is single threaded. It's amazing. And can handle thousands of simultaneous connections. But if code in one of those connections blocks for 1 second then NOTHING else will be done for any other connection during that second.

like image 97
aychedee Avatar answered Sep 30 '22 00:09

aychedee