Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Ratchet WebSocket Server send a message to client itself?

I want to use a Ratchet (http://socketo.me) for permanent connection between iPhone Apps and Server. And I need to exchange data between apps and server.

From this example (http://socketo.me/docs/hello-world) I've found out that I have a function onMessage that will be called when the App send a massage to the server and the server could send a response to the App.

But the server also must have an ability to send data to the app without getting data from app. For example, the connection between app and server has been established. Something happened on the server and we need to send a new data to the app. How can I do it and is it possible?

The main question is how can I send data to the App from the server?

Thank you for any help.

like image 466
lexa Avatar asked Oct 13 '12 15:10

lexa


People also ask

How do I send a WebSocket message?

send() The WebSocket. send() method enqueues the specified data to be transmitted to the server over the WebSocket connection, increasing the value of bufferedAmount by the number of bytes needed to contain the data.

How big can WebSocket messages be?

websockets frame buffer: its size depends both on the size and the number of frames it contains. By default the maximum size is 1MB and the maximum number is 32.

Do websockets queue messages?

Web sockets provide a reliable and easy-to-implement way to connect web clients with business applications which use messaging queues for inter-module communication.

Are websockets guaranteed delivery?

There is no guarantee of the successful delivery of a web socket message to the peer, but if the action of sending a message causes an error known to the container, the API throws it.


1 Answers

That is indeed possible. You need to communicate with the WebSocket server process somehow. You can do that by using some form of message passing, be it RPC or a message queue.

Ratchet itself is based on the React event loop. This means that any form of communication with Ratchet must be integrated with that event loop. On the React homepage you can see some of the integrations that already exist:

  • Predis/Async (you can use redis pub/sub for message passing)
  • DNode-PHP (dnode is a TCP based RPC protocol)
  • React/ZMQ (integrates the event loop with ZeroMQ)
  • React/Stomp (implementation of the STOMP protocol, allowing you to talk to a message such as RabbitMQ)
  • JCook21/ReactAMQP (AMQP bindings for React PHP)

In the Ratchet documentation there is a tutorial on how to use React/ZMQ in order to push messages from anywhere to your WebSocket server.

like image 174
igorw Avatar answered Oct 04 '22 17:10

igorw