Solutions for creating a chat application without constantly querying with ajax

Viewed 276

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)

1 Answers

You'll need to utilize WebSockets: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API

This allows the browser to keep a connection open with the server for constant communication, both to and from the server.

The alternative is polling, which is sending periodic ajax requests, as you described.

From the Mozilla page:

With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.

Related