实时和PHP?

I'm beginner in realtime applications (e.g : Chat, MMORPG), but I'am good in php and ajax. I have programmed a chat using php and ajax(Interval 1000ms).. After, my site has become very heavy with reason of the large pressure on the server!!

I want information about reducing the pressure on the server.

thanks in advance

Look for long-polling ajax

It requests only once a minute (requests aren't 60/minute but 1/minute)

You can also have a look at the ajax push engine to do real time data streaming (they claim that it scales really well). http://www.ape-project.org/

You could try converting the chat to a socket-based web application. I don't know about the differences in performance between using AJAX or using sockets but my using sockets for this sounds more logic to me. :)

You may want to cache results. Setting up memcached (key => value) cache can reduce the database load.

When a new request comes in have your application ask memcache if the value its looking for is in the cache. Otherwise ask your database engine for the value, return the value to the user and cache it for next time.

Interesting PHP library providing developers with tools to create real time, bi-directional applications between clients and servers over WebSockets: http://socketo.me/

Look no further than PubNub!

PHP client libraries are available at https://github.com/pubnub/php (including composer)

and 50+ more (including javascript) can be found here: http://www.pubnub.com/developers/

See if you qualify for the free plan: http://www.pubnub.com/free-evangelism-program/

As far as i known, there are several libraries available to build non-blocking and realtime applications like chat and realtime game.

  1. reactphp, implemented in pure PHP, like nodejs, but the development seems not very active recently.

  2. Skyray, a networking library for PHP writen in C http://github.com/SkyrayLabs/Skyray

    This is a networking library for PHP created by me, it is still under active developing.

  3. swoole, https://github.com/swoole/swoole-src

  4. phpdaemon, https://github.com/kakserpom/phpdaemon

Even the question is old, I would like to share what I found with you.

WebSockets is now supported by all major browsers. So, the easiest way to create a real-time system is by using WebSockets. But, PHP doesn't support WebSockets by default.

But, there's Node.JS to make the task easier. Do I need to reprogram my website in Node.JS? No! There's a smarter solution that I found which helps you to use Node.js together with PHP only for real-time communication.

How does it work?

  1. User connects to the WebSocket server written in Node.js
  2. When we have a notification or update in PHP, we notify the Node.js server via an HTTP POST request
  3. Node.js HTTP server process the request and send the response to connected users via WebSockets.

See Creating a Real-Time Chat App with PHP and Node.js for an example.