PHP:Stomp - Is it possible to catch errors on "send()"? - php

I'm using the PHP Stomp client to send a stomp message.
I would like to leave a persistent connection open, in the background, and send messages occasionally.
However, I can't find a way to handle connection errors if they happen after opening the connection (on send()).
For example, when running:
<?php
$stomp = new Stomp('tcp://localhost:61613');
sleep(5); // Connection goes down in the meantime
$result = $stomp->send('/topic/test', 'TEST');
print "send " . ($result ? "successful\n": "failed\n");
?>
Output: send successful
Even if the connection goes down while in sleep(), send() always returns true.
The docs weren't very helpful, Stomp::error() and stomp_connect_error() also don't help much as they return false.
As a temporary solution, I'm reconnecting before every send().
Is there a better way to catch connection errors?

Found the answer in the specification of the stomp protocol itself:
Any client frame other than CONNECT MAY specify a receipt header with an arbitrary value. This will cause the server to acknowledge receipt of the frame with a RECEIPT frame which contains the value of this header as the value of the receipt-id header in the RECEIPT frame.
So setting a "receipt" header makes the request synchronous, so the connection to the server must be alive.
So the code:
$result = $stomp->send('/topic/test', 'TEST');
print "send " . ($result ? "successful\n": "failed\n");
$result = $stomp->send('/topic/test', 'TEST', array('receipt' => 'message-123'));
print "send " . ($result ? "successful\n": "failed\n");
Gives output:
send successful
send failed
It doesn't seem like the best solution for this case, but it works for me.
If anyone knows a better way I'll be happy to hear it.
Update:
Eventually I switched to Stomp-PHP (a pure PHP client) instead of the Pecl stomp client, which handles it much better.

Related

Stomp Subscribe No Response

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
Send Connection (request?)
Send Subscription
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?
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
Send Connection message ("CONNECT\naccept-version:1.0,1.1,2.0\n\n\x00\n")
Send subscription ("SUBSCRIBE\nid:0\ndestination:/user/queue\nack:auto\n\n\x00\n")
Use the asynchronous Websocket instead of synchronous one
Have a nice day

Make stream_socket_server to stay functional after first received data package

I'm trying to make tcp socket server to create and maintain persistent bidirectional communications, via PHP's stream_socket_server().
Short version of question:
how to have tcp server created with stream_socket_server() staying
alive - not failing to receive data after first successful data
reception which is in my case one single character typed in terminal after telnet
command?
Long version - what exactly I expect to achieve
Just for illustration, look at communication type when telneting some host with smtp server. You type in terminal telnet somehost 25 and get welcome response and (usually) smtp banner. And connection persists. Than you type hello command, and you get, again, response with option to proceed issuing further commands. Again, connection persists, you got response and option to continue. This is exact communication type I'm after.
What have I done so far
this code:
<?php
$server = stream_socket_server("tcp://0.0.0.0:4444", $errno, $errorMessage);
if ($server === false) throw new UnexpectedValueException("Could not bind to socket: $errorMessage");
for (;;) {
$client = stream_socket_accept($server);
if ($client)
{
echo 'Connection accepted from ' . stream_socket_get_name($client, false) . "\n";
echo "Received Out-Of-Band: " . fread($client, 1500) . "\n";
#fclose($client);
}
}
which works fine if you are just trying to send data (once per shot) via another PHP script i.e. stream_socket_client().
Server created by this script fails to maintain connection type I have described. After doing telnet localhost 4444 I'm switched into terminal mode to write message. When I do so, on the server side I got first typed character caught up and - nothing more. Whatever I've tried, I couldn't catch new packages sent from client side by typing. Like stream_socket_accept() blocks or ignores all after first character-data package received.
So, what am I doing wrong - how to solve this issue?
Your program is only doing a single read before it loops back to accept another incoming connection.
The documentation for fread states that it will return as soon as a packet has been received. That is, it won't wait for the full 1500 bytes.
Given the slow speed of human typing, you end up sending a packet with a single character to your server which is returned and then it goes on to accept another incoming connection.
Put a loop around your read as such:
for (;;) {
$client = stream_socket_accept($server);
if ($client) {
echo 'Connection accepted from ' . stream_socket_get_name($client, false) . "\n";
for (;;) {
$data = fread($client, 1500);
if ($data == '') break;
echo "Received Out-Of-Band: " . $data . "\n";
}
fclose($client);
}
}

PHP web service, send response before end of script execution

I have a web service written in PHP to which an iPhone app connects to. When the app calls the service, a series of notification messages are sent to Apple's APNs server so it can then send Push Notifications to other users of the app.
This process can be time consuming in some cases and my app has to wait a long time before getting a response. The response is totally independent of the result of the notification messages being sent to the APNs server.
Therefore, I would like the web service to send the response back to the app regardless of whether the messages to APNs have been sent.
I tried using pcntl_fork to solve the problem:
<?php
...
$pid = pcntl_fork();
if($pid == -1)
{
// Could not fork (send response anyway)
echo "response";
}
else if($pid)
{
// Parent process - send response to app
echo "response";
}
else
{
// Child process - send messages to APNs then die
sendMessageAPNs($token_array);
die();
}
?> // end of script
Unfortunately, the parent process seems to wait for the child process to end before sending the response even though I do not use pcntl_wait in the parent process. Am I doing something wrong or is this normal behaviour? If this is normal then is there another way I can solve this problem?
Thank you!
If you're hosting the PHP process in Apache then you really shouldn't use this: see this for the section that says *Process Control should not be enabled within a web server environment and unexpected results may happen if any Process Control functions are used within a web server environment. *.
You should probably set up a separate daemon in your preferred language of choice and hand the APNS communication tasks off to that. If you really really really must try using ob_flush().
I think you can send the response back before doing the "long" process. Take a look at the flush() function of PHP it'll maybe help

Fetch errors from APNS with PHP

I use a self written script to send push notifications to APNS with PHP. In order to be able to process errors I use the extended format for the Push notifications and would like to fetch results from the stream:
// $apns = a stream_socket_client connection
$apnsMessage = pack('CNNnH*', 1, $i, $pnDetails['expiration_time'], 32, $pnDetails['token']);
$apnsMessage .= pack('n', strlen($pnDetails['payload']));
$apnsMessage .= $pnDetails['payload'];
fwrite($apns, $apnsMessage);
// Check for errors
$errorResponse = #fread($apns, 6)
if ($errorResponse != FALSE) {
$unpackedError = unpack('Ccommand/CstatusCode/Nidentifier', $errorResponse);
}
I have seen a very similar practice in the apns-php project, however, in my case the script always waits indefinitely at the fread line because it tries to read data which is not there (Apple only sends a response if there was an error). I have looking for ways to tell if there is any new data to read from a TCP stream, however, I could find none and the stream callback methods available for HTTP calls are not available for "raw" TCP connections either.
How can I transform my script to make sure it only calls fread when there actually is data to read? How does the apns-php project solve this issue (from what I could tell they were just calling fread as well)?
Figured it out, the final hint came from Erwin. The trick was to deactivate the blocking with stream_set_blocking, now I just need to wait some time before fetching the results with fread to make sure that Apple has enough time to respond.
Are you connecting to the right host ssl://feedback.push.apple.com:2196 ?
They are using the following calls to connect and read data:
stream_context_create -> stream_socket_client -> stream_set_blocking (0) -> stream_set_write_buffer (0) -> while (!feof($socket)) {} -> fread (8192) -> stream_select (with timeout)

How do I generate a connection reset programatically?

I'm sure you've seen the "the connection was reset" message displayed when trying to browse web pages. (The text is from Firefox, other browsers differ.)
I need to generate that message/error/condition on demand, to test workarounds.
So, how do I generate that condition programmatically? (How to generate a TCP RST from PHP -- or one of the other web-app languages?)
Caveats and Conditions:
It cannot be a general IP block. The test client must still be able to see the test server when not triggering the condition.
Ideally, it would be done at the web-application level (Python, PHP, Coldfusion, Javascript, etc.). Access to routers is problematic. Access to Apache config is a pain.
Ideally, it would be triggered by fetching a specific web-page.
Bonus if it works on a standard, commercial web host.
Update:
Sending RST is not enough to cause this condition. See my partial answer, below.
I've a solution that works on a local machine, Now need to get it working on a remote host.
I would recommend doing this via a custom socket via CLI as messing with the apache process could be messy:
#!/usr/bin/php -q
<?php
set_time_limit (0);
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($sock, '1.1.1.1', 8081) or die('Could not bind to address');
socket_listen($sock);
$client = socket_accept($sock);
sleep(1);
$pid = getmypid();
exec("kill -9 $pid");
?>
This will generate the desired error in Firefox as the connection is closed before read.
If you feel insanely daring, you could throw this into a web script but I wouldn't even venture trying that unless you own the box and know what you're doing admin wise.
I believe you need to close the low-level socket fairly abruptly. You won't be able to do it from Javascript. For the other languages you'll generally need to get a handle on the underlying socket object and close() it manually.
I also doubt you can do this through Apache since it is Apache and not your application holding the socket. At best your efforts are likely to generate a HTTP 500 error which is not what you're after.
Update:
This script worked well enough to test our connection-reset workaround, so it's a partial answer.
If someone comes up with the full solution that works on a remote host, I'll gladly mark that as the answer.
The following script works every time when running and tested on the same machine. But when running on a remote host, the browser gets the following last 3 packets:
Source Dest Protocol Info
<server> <client> TCP 8081 > 1835 [RST] Seq=2 Len=0
<server> <client> TCP 8081 > 1835 [RST] Seq=2 Len=0
<server> <client> TCP http > 1834 [ACK] Seq=34 Ack=1 Win=6756 Len=0
As you can see, the RST flag is set and sent. But Firefox fails silently with a blank page -- no messages of any kind.
Script:
<?php
$time_lim = 30;
$listen_port = 8081;
echo
'<h1>Testing generation of a connection reset condition.</h1>
<p><a target="_blank" href="http://' .$_SERVER["HTTP_HOST"]. ':' .$listen_port. '/">
Click here to load page that gets reset. You have ' . $time_lim . ' seconds.</a>
</p>
'
;
flush ();
?>
<?php
//-- Warning! If the script blocks, below, this is not counted against the time limit.
set_time_limit ($time_lim);
$socket = #socket_create_listen ($listen_port);
if (!$socket) {
print "Failed to create socket!\n";
exit;
}
socket_set_nonblock ($socket); //-- Needed, or else script executes until a client interacts with the socket.
while (true) {
//-- Use # to suppress warnings. Exception handling didn't work.
$client = #socket_accept ($socket);
if ($client)
break;
}
/*--- If l_onoff is non-zero and l_linger is zero, all the unsent data will be
discarded and RST (reset) is sent to the peer in the case of a connection-
oriented socket.
*/
$linger = array ('l_linger' => 0, 'l_onoff' => 1);
socket_set_option ($socket, SOL_SOCKET, SO_LINGER, $linger);
//--- If we just close, the Browser gets the RST flag but fails silently (completely blank).
socket_close ($socket);
echo "<p>Done.</p>";
?>

Categories