Why ZEROMQ dont push data every time in Ratchet PHP? - php

I try build real-time application with RatchetPHP and ZeroMQ. When my clients subscribes to my WebSocket, I would want push some data to my Master. But not every request is pushed to my Master. If More specifically, every second request is pushed. When I run my script, and reload connection to websocket several times I ses following output:
Topic manager started!
Master started!
Opened connection!
Master: message handled by :NULL
Opened connection!
Opened connection!
Master: message handled by :NULL
Opened connection!
Opened connection!
Master: message handled by :NULL
Opened connection!
Opened connection!
Master: message handled by :NULL
Opened connection!
It means, that when I connect to my Websocket, only second connection push data to Master. And my code is below:
use React\EventLoop\LoopInterface;
use React\ZMQ\Context;
class Master
{
const MASTER_PORT = 3333;
public function __construct(LoopInterface $loop) {
// Listen for the web server to make a ZeroMQ push after an ajax request
$context = new Context($loop);
$pull = $context->getSocket(\ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:' . self::MASTER_PORT); // Binding to 127.0.0.1 means the only client that can connect is itself
$pull->on('message', array($this, 'onMessage'));
echo 'Master started!' . PHP_EOL;
}
public function onMessage($message) {
echo 'Master: message handled :';
var_dump($message);
}
// ...
}
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\WampServerInterface;
use Ratchet\WebSocket\WsServerInterface;
class Pusher implements WsServerInterface, WampServerInterface {
const WEBSOCKET_PORT = 5555;
/**
* #var Topic[]
*/
protected $topicLookup = array();
protected $pushMaster;
public function __construct()
{
// Push to master
$context = new \ZMQContext();
$this->pushMaster = $context->getSocket(\ZMQ::SOCKET_PUSH, 'workers');
$this->pushMaster->connect("tcp://localhost:" . Master::MASTER_PORT);
echo 'Topic manager started!' . PHP_EOL;
}
/**
* {#inheritdoc}
*/
public function onOpen(ConnectionInterface $conn) {
$conn->WAMP->subscriptions = new \SplObjectStorage;
echo 'Opened connection!' . PHP_EOL;
}
/**
* {#inheritdoc}
*/
public function onSubscribe(ConnectionInterface $conn, $topic) {
echo 'Subscribe for ' . $topic . PHP_EOL;
$topicObj = $this->getTopic($topic);
if ($conn->WAMP->subscriptions->contains($topicObj)) {
return;
}
$this->topicLookup[$topic]->add($conn);
$conn->WAMP->subscriptions->attach($topicObj);
}
protected function getTopic($topic) {
if (!array_key_exists($topic, $this->topicLookup)) {
echo $topic . ' before send' . PHP_EOL;
$this->push(json_encode(['action' => 'start', 'from' => $topic]));
$this->topicLookup[$topic] = new Topic($topic);
}
return $this->topicLookup[$topic];
}
/**
* #param string JSON'ified string we'll receive from ZeroMQ
*/
public function onPush($entry) {
$entryData = json_decode($entry, true);
// If the lookup topic object isn't set there is no one to publish to
if (!array_key_exists($entryData['symbol'], $this->topicLookup)) {
return;
}
$topic = $this->topicLookup[$entryData['symbol']];
// re-send the data to all the clients subscribed to that category
$topic->broadcast($entryData);
}
}
// script
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$pusher = new \app\Pusher();
// Listen for the web server to make a ZeroMQ push after an ajax request
$context = new React\ZMQ\Context($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:' . \app\socket\Pusher::WEBSOCKET_PORT); // Binding to 127.0.0.1 means the only client that can connect is itself
$pull->on('message', array($pusher, 'onPush'));
// Start master for listening messages from Pusher
//$master = new \app\daemon\Master($loop);
// Set up our WebSocket server for clients wanting real-time updates
$webSock = new React\Socket\Server($loop);
$webSock->listen(8081, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect
$webServer = new Ratchet\Server\IoServer(
new Ratchet\Http\HttpServer(
new Ratchet\WebSocket\WsServer(
new \app\socket\WampServer(
$pusher
)
)
),
$webSock
);
$loop->run();
What I do wrong?

Related

Ratchet PHP Websocket Library works fine but causing HTTP code 503 Service unavailable after running for for a few days

I am using Ratchet for Websockets in my application. I am running the Websocket server in supervisor. The application runs well for sometime even when there is high traffic but after some time the server responds with a 503 service unavailable error when I try to access the application in the browser until I restart supervisor and It runs fine again for some time. This happens at times when there is low traffic to the sever so I do not think it a traffic problem.
Could I have missed a configuration somewhere during deployment of Ratchet because it is the only program that I am running in supervisor.
Thanks for your assistance.
I solved this by adding gc_collect_cycles() to the onClose function.
My code now looks like this:
<?php
//Pusher
namespace MyApp;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\WampServerInterface;
class Pusher implements WampServerInterface
{
//Array of channels with connected clients
protected $subscribed_channels= array();
protected $clients=array();
protected $connections;
public function __construct() {
$this->connections = new \SplObjectStorage;
}
//Accepts client connections and adds the channel subscribed to in the subscribed channels
public function onSubscribe(ConnectionInterface $conn, $channel)
{
$channel_id=$channel->getId();
$this->subscribed_channels[$channel_id] = $channel;
$connection_id=$conn->resourceId;
$this->clients[$connection_id]=array('channel'=>$channel);
$channel_subscriptions=$channel->count();
//echo "Client {$connection_id} has subscribed to {$channel_id} channel. {$channel_subscriptions} Cleints Subscribed \n";
}
//Accepts data as json containing the channel being pushed to, and the data being pushed.
public function onPush($request)
{
$request_array = json_decode($request, true);
if(!isset($request_array['channel']))
{
echo"Channel not sent by app \n";
return;
}
//Check if data has been sent by app
if(!isset($request_array['data']))
{
echo"Data not sent by app \n";
return;
}
// If the channel being pushed to has no subscribers, don't push
if (!array_key_exists($request_array['channel'], $this->subscribed_channels))
{
echo"{$request_array['channel']} channel has no subscribers \n";
return;
}
$channel = $this->subscribed_channels[$request_array['channel']];
// Foward the request to all the subscribers in the channel
$data=json_encode($request_array['data']);
$channel->broadcast($data);
//echo "Pushing data to {$request_array['channel']} channel at ".strftime('%Y-%m-%d %H:%M:%S')."\n";
}
public function onUnSubscribe(ConnectionInterface $conn, $channel)
{
$connection_id=$conn->resourceId;
$channel_id=$channel->getId();
$channel_subscriptions=$channel->count()-1;
if($channel_subscriptions==0)
{
unset($this->subscribed_channels[$channel_id]);
}
//echo "Client {$conn->resourceId} has disconnected from {$channel_id} channel. {$channel_subscriptions} Clients subscribed \n";
}
public function onOpen(ConnectionInterface $conn)
{
$this->connections->attach($conn);
}
public function onClose(ConnectionInterface $conn)
{
$connection_id=$conn->resourceId;
$channel=$this->clients[$connection_id]['channel'];
unset($this->clients[$connection_id]);
$this->onUnSubscribe($conn,$channel);
$this->connections->detach($conn);
gc_collect_cycles();
}
public function onCall(ConnectionInterface $conn, $id, $channel, array $params)
{
// In this application if clients send data it's because the user hacked around in console
$conn->callError($id, $channel, 'You are not allowed to make calls')->close();
}
public function onPublish(ConnectionInterface $conn, $channel, $event, array $exclude, array $eligible)
{
// In this application if clients send data it's because the user hacked around in console
$conn->close();
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
echo "An error has occurred: {$e->getMessage()}\n";
}
}
//Web socket server code
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$pusher = new MyApp\Pusher;
// Listen for the web server to make a ZeroMQ push after an ajax request
$context = new React\ZMQ\Context($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
$pull->on('message', array($pusher, 'onPush'));
//Replace the variables $my_ip and $my_port with your server $ip and $port.
$webSock = new React\Socket\Server('$my_ip:$my_port', $loop);
$webServer = new Ratchet\Server\IoServer(
new Ratchet\Http\HttpServer(
new Ratchet\WebSocket\WsServer(
new Ratchet\Wamp\WampServer(
$pusher
)
)
),
$webSock
);
$loop->run();
//My php app code
//You will need the zmq php module in your php for this to work
function push_to_socket($channel,$data)
{
$message = array(
'channel' => $channel
, 'data' => $data
);
$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'my pusher');
$socket->connect("tcp://localhost:5555");
$socket->send(json_encode($message));
}
//My Javascript code
//You need AutobahnJS for this to work.
//You might also need jQuery or else modify the code to eliminate jQuery syntax
//Replace 'channel' with a channel/topic name you want to use.
<script type="text/javascript">
$(document).ready(get_connected);
function get_connected()
{
var hasWs = 'WebSocket' in window || 'MozWebSocket' in window;
if (hasWs)
{
conn = new ab.Session('wss:{my_domain}/wss',
function()
{
conn.subscribe('channel', function(topic,message){
//Process your message
});
},
function() {
//console.warn('Connection closed');
get_connected();
},
{'skipSubprotocolCheck': true}
);
}
}
</script>

Ratchet Push Server Tutorial issues

I am trying to get the ratchet push tutorial to work.
http://socketo.me/docs/push
I am doing exactly what the tutorial is saying but no messages will be received by my subscriber.
My server.php
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$pusher = new MyApp\Pusher;
// Listen for the web server to make a ZeroMQ push after an ajax request
$context = new React\ZMQ\Context($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
$pull->on('message', array($pusher, 'onBlogEntry'));
// Set up our WebSocket server for clients wanting real-time updates
$webSock = new React\Socket\Server('0.0.0.0:8080', $loop); // Binding to 0.0.0.0 means remotes can connect
$webServer = new Ratchet\Server\IoServer(
new Ratchet\Http\HttpServer(
new Ratchet\WebSocket\WsServer(
new Ratchet\Wamp\WampServer(
$pusher
)
)
),
$webSock
);
$loop->run();
My addblog.php
<?php
// post.php ???
// This all was here before ;)
$entryData = array(
'category' => 'kittensCategory'
, 'title' => 'Test'
, 'article' => 'Test'
, 'when' => time()
);
$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'my pusher');
$socket->connect("tcp://localhost:5555");
$socket->send(json_encode($entryData));
And my listener.html
<script src="autobahn.js"></script>
<script>
var conn = new ab.Session('ws://localhost:8080',
function() {
conn.subscribe('kittensCategory', function(topic, data) {
// This is where you would add the new article to the DOM (beyond the scope of this tutorial)
console.log('New article published to category "' + topic + '" : ' + data.title);
});
},
function() {
console.warn('WebSocket connection closed');
},
{'skipSubprotocolCheck': true}
);
</script>
And my Pusher.php
<?php
namespace MyApp;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\WampServerInterface;
class Pusher implements WampServerInterface {
/**
* A lookup of all the topics clients have subscribed to
*/
protected $subscribedTopics = array();
public function onSubscribe(ConnectionInterface $conn, $topic) {
echo "Hello to: ".$topic;
$this->subscribedTopics[$topic->getId()] = $topic;
}
/**
* #param string JSON'ified string we'll receive from ZeroMQ
*/
public function onBlogEntry($entry) {
$entryData = json_decode($entry, true);
echo "gallogallo";
// If the lookup topic object isn't set there is no one to publish to
if (!array_key_exists($entryData['category'], $this->subscribedTopics))
{
return;
}
$topic = $this->subscribedTopics[$entryData['category']];
// re-send the data to all the clients subscribed to that category
$topic->broadcast($entryData);
}
I do not receive any errors or warnings.
When I try to call addblog.php nothing is happening and I do not understand why.
Any hints maybe? I am testing this with XAMPP and Windows 10.
I don't know why, but it is working very well on another system...

Ratchet push integration not working with Laravel

I have successfully integrated the Ratchet chat unto my Laravel app, now I want to integrate the Ratchet push integration but sadly not working. First I have this pusher.php server file on the root directory of my Laravel app project folder
<?php
use App\Http\Controllers\SocketController;
require './vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$pusher = new SocketController();
// Listen for the web server to make a ZeroMQ push after an ajax request
$context = new React\ZMQ\Context($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
$pull->on('message', array($pusher, 'onBlogEntry'));
// Set up our WebSocket server for clients wanting real-time updates
$webSock = new React\Socket\Server($loop);
$webSock->listen(8080, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect
$webServer = new Ratchet\Server\IoServer(
new Ratchet\Http\HttpServer(
new Ratchet\WebSocket\WsServer(
new Ratchet\Wamp\WampServer(
$pusher
)
)
),
$webSock
);
echo "Pusher server is running\n";
$loop->run();
and this controller named SocketController.php
<?php
namespace App\Http\Controllers;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\Topic;
use Ratchet\Wamp\WampServerInterface;
class SocketController implements WampServerInterface
{
/**
* A lookup of all the topics clients have subscribed to
*/
protected $subscribedTopics = array();
public function onSubscribe(ConnectionInterface $conn, $topic) {
echo"on";
$this->subscribedTopics[$topic->getId()] = $topic;
}
/**
* #param string JSON'ified string we'll receive from ZeroMQ
*/
public function onBlogEntry($entry) {
$entryData = json_decode($entry, true);
// If the lookup topic object isn't set there is no one to publish to
if (!array_key_exists($entryData['category'], $this->subscribedTopics)) {
return;
}
$topic = $this->subscribedTopics[$entryData['category']];
// re-send the data to all the clients subscribed to that category
$topic->broadcast($entryData);
echo "A new topic has been push";
}
public function onUnSubscribe(ConnectionInterface $conn, $topic) {
echo "A client has subscribed ".$conn->resourceId."\n";
}
public function onOpen(ConnectionInterface $conn) {
echo "A client is connected ".$conn->resourceId."\n";
}
public function onClose(ConnectionInterface $conn) {
echo "A client has been disconnected ".$conn->resourceId."\n";
}
/* The rest of our methods were as they were, omitted from docs to save space */
/**
* If there is an error with one of the sockets, or somewhere in the application where an Exception is thrown,
* the Exception is sent back down the stack, handled by the Server and bubbled back up the application through this method
* #param ConnectionInterface $conn
* #param \Exception $e
* #throws \Exception
*/
function onError(ConnectionInterface $conn, \Exception $e)
{
// TODO: Implement onError() method.
}
/**
* An RPC call has been received
* #param \Ratchet\ConnectionInterface $conn
* #param string $id The unique ID of the RPC, required to respond to
* #param string|Topic $topic The topic to execute the call against
* #param array $params Call parameters received from the client
*/
function onCall(ConnectionInterface $conn, $id, $topic, array $params)
{
// TODO: Implement onCall() method.
}
/**
* A client is attempting to publish content to a subscribed connections on a URI
* #param \Ratchet\ConnectionInterface $conn
* #param string|Topic $topic The topic the user has attempted to publish to
* #param string $event Payload of the publish
* #param array $exclude A list of session IDs the message should be excluded from (blacklist)
* #param array $eligible A list of session Ids the message should be send to (whitelist)
*/
function onPublish(ConnectionInterface $conn, $topic, $event, array $exclude, array $eligible)
{
// TODO: Implement onPublish() method.
}
}
Yes, I followed everything from http://socketo.me/docs/push. Next, I set up a route and a test controller to be used for pushing an entry.
In the route
Route::get('/test','testController#index');
the test controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use \ZMQContext;
use \ZMQ;
class testController extends Controller
{
public function index(){
$entryData = array(
'category' => 'cat',
'title' => 'title 1',
'article' => 'article 1',
'when' => time()
);
// This is our new stuff
$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'my pusher');
$socket->connect("tcp://localhost:5555");
$socket->send(json_encode($entryData));
dd($entryData);
}
}
and in the client side, I'll connect it to the Ratchet push server.
<script src="http://autobahn.s3.amazonaws.com/js/autobahn.min.js"></script>
<script>
var conn = new ab.Session('ws://localhost:8080',
function() {
conn.subscribe('cat', function(topic, data) {
// This is where you would add the new article to the DOM (beyond the scope of this tutorial)
alert('New article published to category "' + topic + '" : ' + data.title);
});
},
function() {
console.warn('WebSocket connection closed');
},
{'skipSubprotocolCheck': true}
);
</script>
Now, I run the pusher.php by
cd c:/wamp/www/laravelApp php pusher.php
and in the console, I did log that the client side is connected but when I visit localhost:8000/test from my browser (push entry), it did not the
echo "A new topic has been push";
as it supposed to because I set it up here
public function onBlogEntry($entry) {
$entryData = json_decode($entry, true);
// If the lookup topic object isn't set there is no one to publish to
if (!array_key_exists($entryData['category'], $this->subscribedTopics)) {
return;
}
$topic = $this->subscribedTopics[$entryData['category']];
// re-send the data to all the clients subscribed to that category
$topic->broadcast($entryData);
echo "A new topic has been push";
}
so it means, subscribing to a category is not working, any ideas, help please?

Ratchet, push integration, not working

I am using ratchet along with laravel.
This is my main socket server:
<?php
/**
* Created by PhpStorm.
* User: harshvardhangupta
* Date: 27/05/16
* Time: 1:40 PM
*/
use App\Http\Controllers\SocketController;
require './vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$pusher = new SocketController();
// Listen for the web server to make a ZeroMQ push after an ajax request
$context = new React\ZMQ\Context($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
$pull->on('message', array($pusher, 'onBlogEntry'));
// Set up our WebSocket server for clients wanting real-time updates
$webSock = new React\Socket\Server($loop);
$webSock->listen(8080, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect
$webServer = new Ratchet\Server\IoServer(
new Ratchet\Http\HttpServer(
new Ratchet\WebSocket\WsServer(
new Ratchet\Wamp\WampServer(
$pusher
)
)
),
$webSock
);
$loop->run();
and this is my socket controller:
<?php
namespace App\Http\Controllers;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\Topic;
use Ratchet\Wamp\WampServerInterface;
class SocketController implements WampServerInterface
{
/**
* A lookup of all the topics clients have subscribed to
*/
protected $subscribedTopics = array();
public function onSubscribe(ConnectionInterface $conn, $topic) {
echo"on";
$this->subscribedTopics[$topic->getId()] = $topic;
}
/**
* #param string JSON'ified string we'll receive from ZeroMQ
*/
public function onBlogEntry($entry) {
$entryData = json_decode($entry, true);
// If the lookup topic object isn't set there is no one to publish to
if (!array_key_exists($entryData['category'], $this->subscribedTopics)) {
return;
}
$topic = $this->subscribedTopics[$entryData['category']];
// re-send the data to all the clients subscribed to that category
$topic->broadcast($entryData);
}
public function onUnSubscribe(ConnectionInterface $conn, $topic) {
}
public function onOpen(ConnectionInterface $conn) {
echo"open";
}
public function onClose(ConnectionInterface $conn) {
echo "close";
}
/* The rest of our methods were as they were, omitted from docs to save space */
/**
* If there is an error with one of the sockets, or somewhere in the application where an Exception is thrown,
* the Exception is sent back down the stack, handled by the Server and bubbled back up the application through this method
* #param ConnectionInterface $conn
* #param \Exception $e
* #throws \Exception
*/
function onError(ConnectionInterface $conn, \Exception $e)
{
// TODO: Implement onError() method.
}
/**
* An RPC call has been received
* #param \Ratchet\ConnectionInterface $conn
* #param string $id The unique ID of the RPC, required to respond to
* #param string|Topic $topic The topic to execute the call against
* #param array $params Call parameters received from the client
*/
function onCall(ConnectionInterface $conn, $id, $topic, array $params)
{
// TODO: Implement onCall() method.
}
/**
* A client is attempting to publish content to a subscribed connections on a URI
* #param \Ratchet\ConnectionInterface $conn
* #param string|Topic $topic The topic the user has attempted to publish to
* #param string $event Payload of the publish
* #param array $exclude A list of session IDs the message should be excluded from (blacklist)
* #param array $eligible A list of session Ids the message should be send to (whitelist)
*/
function onPublish(ConnectionInterface $conn, $topic, $event, array $exclude, array $eligible)
{
// TODO: Implement onPublish() method.
}
}
There is another script that calls :
$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'my pusher');
$socket->connect("tcp://localhost:5555");
$socket->send("okay");
die("okay");
First I run the client code in a browser(note, its the same machine as server):
<script src="http://autobahn.s3.amazonaws.com/js/autobahn.min.js"></script>
<script>
var conn = new ab.Session('ws://localhost:8080',
function() {
conn.subscribe('kittensCategory', function(topic, data) {
// This is where you would add the new article to the DOM (beyond the scope of this tutorial)
console.log('New article published to category "' + topic + '" : ' + data.title);
});
},
function() {
console.warn('WebSocket connection closed');
},
{'skipSubprotocolCheck': true}
);
</script>
this connection works, as I am able to see the log message in my server console output.
However, when I call the script that is supposed to send data to the client, the client does not receive it. No log messages are produced in client browser.
To explain why your configuration was not working.
You are sending the message "okay" to the pushserver from the piece of code you have
$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'my pusher');
$socket->connect("tcp://localhost:5555");
$socket->send("okay"); -- HERE --
die("okay");
and in this piece of code you receive that call, this method expects you to send the $entryData['category'] as the 'channel' you want to send the data to
public function onBlogEntry($entry) {
$entryData = json_decode($entry, true);
// If the lookup topic object isn't set there is no one to publish to
if (!array_key_exists($entryData['category'], $this->subscribedTopics)) {
return;
}
$topic = $this->subscribedTopics[$entryData['category']];
// re-send the data to all the clients subscribed to that category
$topic->broadcast($entryData);
}
Although the client is connected to the kittensCategory
conn.subscribe('kittensCategory', function(topic, data){
What you actually should do is send a correct object to the pushserver so that the websocket would know where to send the data to.
$entryData = array(
'category' => 'kittensCategory',
'data' => 'hello'
);
this code would send your data to the kittensCategory
If you need more information please let me know
After trying this for a few hours, I read somewhere to enable debug using ab.debug(true,true).
After that, I see that messages are indeed getting received.

Basic react http server and websocket server at the same time

I managed to create a web service that listens on port 80 for http requests and handles ajax calls and also long polling.
But I'm stuck at creating a similar websocke server in the same php file
<?php
require_once('vendor/autoload.php');
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
// Store the new connection to send messages to later
$this->clients->attach($conn);
$conn->send('aa');
echo "New connection! ({$conn->resourceId})\n";
}
public function onMessage(ConnectionInterface $from, $msg) {
$numRecv = count($this->clients) - 1;
echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
, $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
foreach ($this->clients as $client) {
if ($from !== $client) {
// The sender is not the receiver, send to each client connected
$client->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn) {
// The connection is closed, remove it, as we can no longer send it messages
$this->clients->detach($conn);
echo "Connection {$conn->resourceId} has disconnected\n";
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
$loop = React\EventLoop\Factory::create();
$swsa=new Chat;
$webSock = new React\Socket\Server($loop);
$webSock->listen(8687, '127.0.0.1'); // Binding to 0.0.0.0 means remotes can connect
$webServer = new Ratchet\Server\IoServer(
new Ratchet\Http\HttpServer(
new Ratchet\WebSocket\WsServer(
$swsa
)
),
$webSock
);
$app = function ($request, $response) {
//some code here to manage requests
$response->writeHead(200,array('Content-Type'=>'application/json'));
$response->end(json_encode(array('ok'=>'ok')));
};
$socket = new React\Socket\Server($loop);
$http = new React\Http\Server($socket, $loop);
$http->on('request', $app);
$socket->listen(8485);
$loop->run();
Code in browser:
var wsUri = "ws://127.0.0.1:8687/";
websocket = new WebSocket(wsUri);
This triggers an error in the browser
Firefox can't establish a connection to the server at ws://127.0.0.1:8687/
Your IoServer initialization is incorrect. If you check the code for IoServer class, you will find:
public function __construct(MessageComponentInterface $app,ServerInterface $socket, LoopInterface $loop = null)
and
public function run() {
if (null === $this->loop) {
throw new \RuntimeException("A React Loop was not provided during instantiation");
}
// #codeCoverageIgnoreStart
$this->loop->run();
// #codeCoverageIgnoreEnd
}
The IoServer class object has no loop assigned to it and hence it fails to execute. Try passing loop as your third argument.

Categories