Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this questionWhat technology to use for chat? I would like to create an open connection.
When I put a new message to the database. I want to automatically without using the timer and making loops came a new message to the browser.
I have Linux web hosting with a MySQL database.
I tried to make retrieving new messages and use the timer. Every three seconds I am using Ajax retrieve data. This solution seems to me inefficient, so looking for others.
PHP is a server-side scripting language, which means all the PHP is processed before the page even loads. In order to generate a chat-like environment, you would need to use Javascript to establish an open connection to the back-end (the PHP part). There are many methods to doing this, including polling (timers) and sockets (much more complicated).
The best way I know of to handle a chat-like service using Javascript would be to check out Node.js and its capabilities, specifically demonstrated as a chat room here: http://chat.nodejs.org/.
The problem with NodeJS and persistent connections in general is that most cheap hosting providers don't allow you to have persistent connections open. You would need to pony up for a higher-cost dedicated server. There are, I believe, hosts that specifically allow NodeJS-type services in their environments, but I don't know of any off the top of my head.
You might need to implement COMET technology. It allows to make long pooling requests. When one request is done you can start another one. In COMET connection is always open.
In PHP you can do that creating infinity loop, while(true)
for example and break connection when you need.
精彩评论