Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous chat [closed]

Tags:

ajax

php

mysql

I was following a PHP-MySQL shoutbox tutorial that covered just sending and storing messages in the database. After I finished it, temporarily I chose to refresh the list of messages every 5 seconds or every time you send a new one using AJAX. But this would be too inefficient and server consuming(practically a low-intensity DDOS) in real life applications. So how do I refresh the list of new messages just when necessary? More precisely, how do I get notified that a new message was sent exactly when this happens so that I can display it?

like image 758
chubakueno Avatar asked Sep 14 '13 22:09

chubakueno


People also ask

What does asynchronous chat mean?

Asynchronous Messaging is a communication method where participants on both sides of the conversation have the freedom to start, pause, and resume conversational messaging on their own terms, eliminating the need to wait for a direct live connection (aka synchronous messages).

Is chat asynchronous or synchronous?

Synchronous messaging requires customer time and attention for the duration of the chat. Asynchronous messaging, meanwhile, lets customers simply message in a spare moment. Then, they can get on with their day. There's no time commitment because the chat slots around their life.

What is an asynchronous message call?

What is asynchronous messaging? While synchronous messaging is a live person-to-person conversation, asynchronous communication doesn't require both parties to be present and speaking at the same time. This is great for the customer because they are able to start, pause, and resume a conversation around their life.

What is an example of an asynchronous message?

In a nutshell, asynchronous communication is any communication that does not take place in real-time. Emails, forum comments, corporate intranet, and even Asana or Trello boards serve as examples of asynchronous communication we deal with every day.


1 Answers

A solution to your problem is called long polling, you'll find more information in this SO question.

The idea is to load information with AJAX as you currently do, but the server will not return a response immediately if there's nothing to return. Instead it will just keep the connection open for a predefined number of seconds before returning an empty response, or return as soon as a message becomes available. The max response time should be long enough to make it worthwhile, but not too long to risk a timeout on the client side - something around 20s should be fine.

Although this solution allows you to reduce the number of HTTP calls to your server, it just shifts the problem: you PHP script, while waiting for a message to be available, still needs to poll your database. If you're expecting moderate traffic, you'll be fine. But if you want to be able to seriously scale, you'll have to look for another solution.

The best solution would be to use a proper message queue instead of a database, such as Amazon SQS or IronMQ.

These will scale without limit, and will offer features such as long polling (not sure about IronMQ, but SQS definitely does).

like image 55
BenMorel Avatar answered Sep 23 '22 14:09

BenMorel