John Davidson

php - Solutions for creating a chat application without constantly querying with ajax

0 comments
Message:


I recently need to work on a project which involves having a chat. This chat must update in real-time and it is estimated to be used by more than 9000 users at the same time. I have done some researching on how to do that and came to a conclusion: Use ajax


While I researched on ajax, I found a problem:


Problem 1:


If there are a lot of users where the browser is constantly creating ajax call for a file to get the database chat content, wouldn't that put a lot of strain on the server and eventually won't it collapse?


There are a lot of libraries out there which maybe can fullfil my needs but I wanted to start from scratch, is it possible?


Take an example, whatsapp: if you open dev tools you don't see it making ajax calls but when I receive messages, it also doesn't makes the call. facebook on the other hand will get ajax call when users receive a message.


PS: I am not looking for the code, I just want a way to do it. I can code it myself. (I am using php with mysqli)



My Answer:
One solution to avoid constantly querying the server with ajax calls is to use WebSockets. WebSockets allow for real-time communication between the client and server without the need for constant polling. This can greatly reduce the strain on the server and provide a more efficient way to handle real-time updates in a chat application.

Another option is to implement long polling, where the client makes a request to the server and the server holds the connection open until new data is available. This can be a more efficient way to handle real-time updates compared to constantly polling the server with ajax calls.

You can also consider using a message queue system like RabbitMQ or Redis to handle the messaging between users in a more efficient and scalable way.

Overall, there are several ways to create a real-time chat application without constantly querying the server with ajax calls. It's important to consider the scalability and efficiency of your solution to ensure it can handle a large number of users simultaneously.

Rate this post

3 of 5 based on 7221 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us