Best approach for (cross-platform) real-time streaming in PHP?

I was wondering how to make "true" (semi) streams of data in real time using PHP.

Possible applications: chats, auctions, games, etc.

By "truth" I mean that the data is not just written somewhere and constantly polled, but actually flows for the client somehow.

By "semi" I mean that this is normal if only the flow from server to client is in real time and the messages from client to server are not.

For communication between the client and the server, I would like to stick to simple HTTP (AJAX), and not another protocol.

Streaming to a client using HTTP is possible by manually flushing the output buffer.

The question is what to connect this script to the server?

And as soon as he connected, to make the lock read, not a poll for changes.

The shared memory extension (shmop) will work, but it is not cross-platform.

Maybe memcached will work? But I'm not sure if there is a way to do read locks, so it comes down to polling again - although I'm sure memcached is pretty fast, I just don't like the idea of ​​continuous polling.

Any ideas?

+7
source share
3 answers

PHP is not well suited for real-time streaming. PHP is very slow and not designed to create multi-threaded applications. You are better off implementing a full-blown socket server in Python or Java.

However, I highly recommend checking out NodeJS: http://nodejs.org/

It uses an asynchronous event-based model for I / O, rather than blocking threads in an event loop. NodeJS servers are written in Javascript. NodeJS is fast, scalable, and has a low learning curve.

Clients will connect to the NodeJS HTTP server using long Ajax polling requests. PHP can directly connect to NodeJS and push notifications. Or PHP can write to a message queue or database, memcache, etc., And NodeJS will try these data stores for updates and send new messages to clients.

You may need to write your own daemon to switch between NodeJS and MySQL, memcached, etc. when polling for updates. NodeJS will keep the socket open with the daemon process. The daemon process will test the data warehouse for updates and send updates to NodeJS. The NodeJS HTTP server then sends these updates to clients.

See this tutorial for real-time streaming Twitter implementation: http://net.tutsplus.com/tutorials/javascript-ajax/learning-serverside-javascript-with-node-js/

+5
source

If you use HTML and Javascript, then you need WebSockets. If it is Flash or something else, then ordinary TCP sockets.

The idea is that you run a server file (written in PHP) that is waiting for connections. Once it connects to one or more clients, data can be shifted in both directions. There are several PHP WebSocket projects. Check this:

http://code.google.com/p/phpwebsocket

In addition, there is a framework called Skeleton, which I contributed to it, has a built-in library of WebSocket servers. However, in unstable stages.

http://code.google.com/p/skeleton

Unfortunately, WebSockets are still a new technology, so they are not universally supported. As @Christian pointed out, you can use the Socket.IO library.

+2
source

If you want to communicate between PHP and another language (for example, with a C ++ application), you can look in Apache Thrift ( http://thrift.apache.org/ ). Apache Thrift is widely used on Facebook for "scalable development of multilingual services."

Edit: I would probably use Apache Thrift to communicate with a Twisted application listening on port 80, and so that browsers connect to Twisted using a long poll or websocket. You can also take a look at Socket.IO , an implementation of a web-based Internet socket created for real-time applications.

Basically, you will have to push the application to your Twisted web server using Thrift, and then pass the message to any open connections.

  • Christian
0
source

All Articles