Bashkir
Bashkir

Reputation: 33

Sending message to Ratchet WebSocket Server from another script

I'm not 100% sure this is worded right, but I have a Ratchet WebSocket server working correctly as a chat service. However, I want to, when a user posts a new thread on the forums, have the server automatically post a message into the chat to notify them all of this new post.

I want to do this via a quick TCP connection upon the creation of this thread. I'm still somewhat new to sockets and this area of server coding. Is there an easy way that PHP can ignore HTTP overhead in connecting to this same-server socket and simply sending a message?

Here's the code I've tried to use as a test, but ratchet does not even say it received a connection or message (ADDR and port are correct, socket_connect returns TRUE):

error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
$string = "Hello, a new post has been BLAH";
$Socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$Status = socket_connect($Socket, "***", "***");
$Bytes = socket_write($Socket, $string, strlen($string));
socket_strerror(socket_last_error());
socket_close($Socket);

Upvotes: 2

Views: 3109

Answers (1)

mbonneau
mbonneau

Reputation: 617

The solution suggested on the Ratchet site is to use ZeroMQ to have your synchronous php (web server that is doing the database work for the new post) push the message to the Ratchet server.

They have some pretty good docs at http://socketo.me/docs/push

Upvotes: 0

Related Questions