简体   繁体   中英

Ratchet PHP - Push messaging service

Most of the examples I'm looking at with Ratchet are for chat services, and I'm currently building an application where the user logs in and receives notifications from the server based on their user ID.

I have the basic tutorials working, however I'm struggling to understand a few points:

When the onOpen() method is called, I set the $conn object into an array like so:

$this->clients[$conn->resourceId]['conn'] = $conn;

In my Javascript, within the onopen function I also send a JSON payload with the send function. My server pics this up and stores it like so, in the onMessage method:

$this->clients[$conn->resourceId]['json'] = $json;

So now I have my connected users stored in an array, how do I send a message to a specific user? I've looked into onSubscribe and the broadcast to no avail, but don't really understand what the WampServerInterface is supposed to be used for?

Since my own class is a running script, I obviously can't create a new instance of it anywhere else. As my application will be sending user specific updates I need some sort of way to do this following:

Grab the currently connected users using another script, process these somewhere and return any updates they might have, every 60 seconds. Now although this might sound like polling, it would only be one connection doing this and so wouldn't be intensive on the server - at least that's what I think. So how can I interact my running server script with other "static" PHP scripts elsewhere?

Thanks

The $conn parameter is a implementation of the ConnectionInterface - it has a send() method. So that's how you can send messages to the client.

Ratchet (via React.PHP) also supports timers . So if there's no external dependency, you can just use $loop->addPeriodicTimer() to send messages to every client every 60 seconds.

If you need to send messages based on some external dependencies (like a web server request or a cron script), use ZMQ ( Ratchet docs ). It's easy.

Check out my slides about WebSockets in PHP . The notes are in Czech, but you can find the source code examples useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM