Web Chat Application using Long-Polling Technology

2020-06-23 09:48发布

问题:

We are using a chat system in our company which is build in Ajax, PHP and jQuery. Currently it refreshing in every second both in admin end and user end to pull data from mysql. Which makes the system inefficient and slower.

I want to pull data from mysql only when an user or admin send a message. But the problem is : How I will check from admin end that an user send a message (in real time)? Is it possible in Ajax to send data/request 2 different PHP pages simultaneously in real time? This may be a solution.

Or Any Idea to make efficient chat system? We don't want to use any third party chat solution. Any Idea/help will be highly appreciated.

Thanks,
Rejoanul Alam

回答1:

jQuery Long Polling

This is possible by jQuery/PHP/MySQL with long polling. A sample implementation available in this url: http://sugunan.com/sites/chat/chatterFront.html

Open this in two different browser or machines. And provide the name and start the chat.

The source code of this chat tool can be downloadable at this url: http://www.sugunan.com/sites/chat/source.zip

Node.js

If you are owning the server you can go for node.js faye based implementation: http://faye.jcoglan.com/node.html

This is a push notification technology. So it wont ping the server frequently. But it will deliver the message instantly. It can be integrated with PHP if you want to persist the message on server.



回答2:

Yes, there are some ways,
They are called comet, sometimes reverse ajax, google it ;)

on a shared hosting use long polling,
on dedicated host or VPS use sockets.