How to store real-time chat messages in database?

2024/11/14 2:40:37

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.

Answer

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.

https://en.xdnf.cn/q/71834.html

Related Q&A

Selectively import from another Jupyter Notebook

I arranged my Jupyter notebooks into: data.ipynb, methods.ipynb and results.ipynb. How can I selectively import cells from data and methods notebooks for use in the results notebook?I know of nbimport…

supervisord event listener

Im trying to configure an event listener for supervisord but cant get it to work. I just want to listen for PROCESS_STATE changes and run some python code triggering an urllib2request.In my .conf I hav…

Integration of Java and Python Code in One Eclipse Project

I am writing a compiler in Python using Eclipse with PyDev. Ive come to a stage where I needed to write some code in Java. Im wandering if there is a way of combining these into a single project, bec…

formatting of timestamp on x-axis

Im trying to format the x-axis in my weather data plot. Im happy with the y-axis but all my tries to get the x-axis into a decent, human-readable format didnt work so far. So after several hours of tri…

How can I set the row height in Tkinter TreeView?

I wrote a small app recently that needs to be cross-platform. I used Python and Tkinter for the GUI.It works great but recently I got a new laptop with a hiDPI screen and it seems to mess up the TreeVi…

Is replace row-wise and will overwrite the value within the dict twice?

Assuming I have following data set lst = [u, v, w, x, y] lst_rev = list(reversed(lst)) dct = dict(zip(lst, lst_rev))df = pd.DataFrame({A:[a, b, a, c, a],B:lst},dtype=category)Now I want to replace the …

Python requests, how to add content-type to multipart/form-data request

I Use python requests to upload a file with PUT method.The remote API Accept any request only if the body contains an attribute Content-Type:i mage/png not as Request Header When i use python requests…

Django Year/Month based posts archive

im new to Django and started an application, i did the models, views, templates, but i want to add some kind of archive to the bottom of the page, something like this http://www.flickr.com/photos/ion…

ValueError: You are trying to load a weight file containing 6 layers into a model with 0

I have a simple keras model. After the model is saved. I am unable to load the model. This is the error I get after instantiating the model and trying to load weights:Using TensorFlow backend. Tracebac…

cProfile with imports

Im currently in the process of learn how to use cProfile and I have a few doubts.Im currently trying to profile the following script:import timedef fast():print("Fast!")def slow():time.sleep(…