2

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

Alias
  • 2,983
  • 7
  • 39
  • 62

1 Answers1

3

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.

Ondřej Mirtes
  • 5,054
  • 25
  • 36
  • Cheers, `$conn->send('Hello')` worked although I swear I tried it before! Those slides were handy, although only know English :) – Alias May 26 '14 at 13:26
  • Ah see what you mean now, the docs do explain it but they're pretty poor. – Alias May 26 '14 at 13:57
  • 1
    To save asking another question, any idea what the second parameter is for in getSocket using ZMQContext? I.e. `getSocket(ZMQ::SOCKET_PUSH, 'Pull Notification');` - where is "Pull Notification" used? – Alias May 26 '14 at 17:39
  • Lol no worries, also any clues on http://stackoverflow.com/questions/23875497/react-zmq-ratchet-websocket-server-response would be awesome :D – Alias May 26 '14 at 17:56
  • Didn't finish reading yet, but this: $push->on('Push Notification', array($pusher, 'onNewPush')); -- should be on('message', ... – Ondřej Mirtes May 26 '14 at 17:58
  • If my method returns a value, how do I get that back? I really don't understand this "on" bit :( – Alias May 26 '14 at 19:11
  • 1
    I also have problem with sending message to specific user, I posted question https://stackoverflow.com/questions/49418640/ratchet-send-message-from-server. Problem is i can't get list of connected users from another script – squareCircle Mar 23 '18 at 00:30