Php Sockets - Push From Server

Hi,

I have been reading about PHP sockets recently and I do not understand if it is possible to do real push messages from a PHP server to a client.

All of the tutorials out there talk about sending push messages, yes. However the push messages require some kind of interaction from the client, either by a connection or a message from the client. So that is really request / response almost.

I am wondering whether this is because PHP is single threaded?

I need to make a PHP server that will accept incoming connections from clients.

The PHP server will constantly check for updates in the DB.

If any updates are found it will message the clients.

The problem is it needs to check for these updates without any interaction from the client. It is not …

Client -> Connect

Client -> Check for updates

Server -> Here are the updates

It is …

Client -> Connect

Hours or days later …

Server -> Here are the updates

Not based on the connect action, the updates could come hours or days later etc, in the meantime other clients could have connected or disconnected from the server.

All of the tutorials seem to imply that interaction from the client is required for PHP to send these updates etc.

James.

I think you are missing the entire point of sockets or the like.

If you have no client then you have no connection, is that simple, that’s how everything works on this internet.

Simple, use timestamps and statuses in database, and based on them find out what was the last update the client received and when he connects, send all the new updates, that’s all, simple.

Right OK, but what about if there has been further updates since the client has connected?

Say the client connects at 12:00 and stays on until 15:00, but there was an update at 13:00. How will the client get the update?

not exactly. he confused "sockets" with "websockets".

Sockets are low level communication slots used by TCP/IP. And PHP has support for such low level connections.

WebSockets are mostly used to maintain persistent connection between browser and server so that server can push messages to such client. But their implementation is on higher level in network stack than plain sockets.

There are WebSocket servers which handle WebSockets implemented with PHP (like ratchet http://socketo.me/) but it will always be separate WebSocket server and you have to provide some integration with your application served as regular web pages.

I’ve been down this route before and couldn’t find an easy way to provide this functionality from the web server. I ended up using PubNub, which abstracts the complexity away for you. It handles maintenance of connections. When you want to publish a change, you connect to the service and send the information, then their server distributes it to all of the connected subscribers (almost instantly).

If you’re concerned about security, you could just publish a message telling the client application that updated information is available and that it should send a request to the server.

Hi,

Ok thanks for the responses.

James.