0

I want to subscribe the Spring framework WebSocket and receive the reply.

According to my target WebSocket server, the communication is done using STOMP publish protocol (Build based on Java Springframework API) https://stomp.github.io/stomp-specification-1.1.html

Now the client that I am working on, is build based on PHP and using https://github.com/Textalk/websocket-php/ for websocket client.

My idea to receive the server response is to follow the STOMP over Websocket technique based on this guy's answer Websocket Client not receiving any messages.

Using the current websocket client, I perform these steps

  1. Send Connection (request?)

  2. Send Subscription

  3. Actively receive the reply

     $client = new WebSocket\Client($ws_url);
     //Step 1 Inintate connection;
     $open_msg = "CONNECT\naccept-version:1.0,1.1,2.0\n\n\x00\n";
     //Step 2 Subscribe Request;
     $client->send($open_msg);
     $subs = "SUBSCRIBE\nid:0\ndestination:/user/queue\nack:auto\n\n\x00\n";
     $client->send($subs);
     while (true) {
           try {
               $message = $client->receive();
               echo $message;
               // Act[enter image description here][4] on received message
               // Later, Break while loop to stop listening
           } catch (\WebSocket\ConnectionException $e) {
               // Possibly log errors
           }
         }
     $client->close();
    

The connection (Step 1) is done and tested.

current send and receive result image

Since it is running on the loop, the Received is always printed.

Does anyone know why the API did not send reply?

Bhavya
  • 63
  • 6

1 Answers1

1

It turns out, I have to implement the other Websocket library instead

Instead of using https://github.com/Textalk/websocket-php/ , I moved on and use https://github.com/ratchetphp/Pawl

I don't know what just happened. But I think Textalk is synchronous websocket library and ratchet is asynchronous websocket library.

My current hypothesis is whenever you want to do Stomp over websocket, make sure

  1. Send Connection message ("CONNECT\naccept-version:1.0,1.1,2.0\n\n\x00\n")
  2. Send subscription ("SUBSCRIBE\nid:0\ndestination:/user/queue\nack:auto\n\n\x00\n")
  3. Use the asynchronous Websocket instead of synchronous one

Have a nice day

  • texttalk works but you just need to wait between calls you can see a working sample at https://stackoverflow.com/questions/65046125/is-there-stomp-over-websocket-in-php/75098317#75098317 – open-ecommerce.org Jan 18 '23 at 16:51