Ratchet Websocket for raspberry pi - php

I have written a websocket using ratchet. The webserver is nginx and lives on a raspberry pi. The raspberry pi has been connected to the internet with a router through port forwarding. When I access the site at work, all works well. The websocket connects, all the webpages launch. When I try to access the webpage outside of work the webpage works except the websocket. The worst part is I can't even debug it because I need to be outside the local network for the websocket to fail (i.e not at work). At work, I can connect to the server using two IP addresses, the private IP address that is only accessible locally and the public IP address that can be accessed from anywhere. Both IP addresses properly launch the websocket. When I am not at work, I can only access the webserver on the Public IP address and the websocket does not work.
I have been trying to make this work for a day and a half straight now with no success. Does anybody have any suggestions? Even to help me identify the problem?
The websocket code follows the Ratchet Push Server tutorial:
<?php
require '/var/www/html/vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$pusher = new MyApp\Pusher;
$context = new React\ZMQ\Context($loop);
$pull->bind('tcp://127.0.0.1:5555');
$pull->on('message',array($pusher, 'onBlogEntry'));
$webSock = new React\Socket\Server($loop);
$webSock->listen(443, '0.0.0.0');
$webServer = new Ratchet\Server\IoServer(
new Ratchet\Http\HttpServer(
new Ratchet\Websocket\WsServer(
new Ratchet\Wamp\WampServer(
$pusher
)
)
),
$webSock
);
$loop->run();
?>
The client side code is:
var conn = new ab.Session('ws://privateIPAddress:443',
function (){
console.log("Here");
conn.subscribe('client',function(topic,data) {
console.log("hey");
...
});
},
function() {
console.warn('Websocket connection closed');
},
{'skipSubprotocolCheck': true}
);
I suspect the issue is a security setting since both the public and private IP addresses work when I am at the work site.

This is a very late answer to my own question but in case anyone is still puzzling over the same issues. The answer is don’t use ratchet. Use nodejs with socket.io. All your troubles will fly away. Socket.io which also has a java implementation is simply a more developed package for WebSockets.

Related

Autobahn-js failes to connect to Ratchet Server (Wamp)

I've already checked plenty of questions and tutorials and I'm following the official documentation from socketme portal. I'm running this on XAMPP/Localhost.
Websocket works, when I'm not using the WAMP interface. Otherwise, it fails to connect and doesn't state any reason.
Error is: "app.js:6200 WebSocket connection to 'ws://localhost:8080/socket' failed: connection closed unreachable"
Server
$server = new \Ratchet\App('localhost', 8080);
$server->route('/socket', new WebSocketHandler, array('*'));
$server->run();
WebSocketHandler is a basic implementation of WampServerInterface and only includes the abstracted methods without any change.
On the client side, I'm doing the following to try a connection via autobahn-js:
var connection = new Autobahn.Connection({
transports: [{
type: 'websocket',
port: 8080,
host: 'localhost',
url: 'ws://localhost:8080/socket'
}],
realm: 'realm1'
});
connection.onopen = function(session) {
app.content = app.content + `connected!`
};
connection.onclose = function(reason, details) {
app.content = app.content + `onclose!`
}
connection.open();
I've done something wrong in this implementation, but I can't figure it out. I've tried avoiding the ->route approach and using the following vanilla approach:
$server = IoServer::factory(
new HttpServer(
new WsServer(
new WampServer(
new WebSocketHandler()
)
)
),
8080
);
But, it didn't work.
Any help is much appreciated.
You need Autobahn.js 0.8.2 to connect it to Ratchet (WAMP v1) and it's a pretty old implementation and also ab 0.8.2 is extremely old.
You should navigate your work to Thruway. I tried doing it, but I couldn't find any custom implementation of Thruway RatcherRouter (onMessage, onOpen, etc.), therefore, I decided to do my own (pretty bad) implementation of Ratchet.
TL;DR: Switch to Thruway.

how to make client connect to ratchet on live server?

I've googled about this for so many days and till now the client (browsers) cannot connect to the server.
But the server can run. I think its because its connected to itself(localhost).
I did find ratchet documentation which says:
If you want to open Ratchet up (not behind a proxy) set the third
parameter of App to '0.0.0.0'.
http://socketo.me/docs/troubleshooting
so i tried this in my server.php file. (doesn't work)
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8180,
'0.0.0.0'
);
Next i tried changing the app.php file which located here(doesn't work):
/vendor/cboden/ratchet/src/Ratchet/App.php
public function __construct($httpHost = '0.0.0.0', $port = 8180, $address = '0.0.0.0', LoopInterface $loop = null) {......
Then,i tried changing the port to something else. again the server can run but the client cannot connect.
I referred all these:
How to run Ratchet remotely or on a server?
How to run Ratchet remotely or on a server?
https://github.com/ratchetphp/Ratchet/issues/394
Someone please help. All I want is for the client to be able to connect to the ratchet websocket which is running on the server.

Lost messages in PUSH/PULL pattern ( Ratchet + PHP + ZeroMQ push integration )

I'm creating a chat on my website, a push notifications system, users activity widget ( updating on the fly ) etc.
My website is built on PHP, so I decided to use Ratchet as a websocket server for my tasks. I've installed all required components and I learned the guide on http://socketo.me/docs/push and started to code.
This is inside a ChatMsg( $item ){...} method in the model.php file. It creates a PUSH socket-access-point archetype and sends a message with JSON-data to server via ZeroMQ after inserting a new item in a database:
$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'my pusher');
$socket->connect("tcp://localhost:5555");
$socket->send(json_encode($sData));
Next is my push-server.php, creating only one PULL socket access-point archetype and waiting for new messages, which will be transferred to the pusher script, broadcasting new notifications, chat messages and other events to clients.
<?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->setSockOpt(ZMQ::SOCKOPT_HWM, 0);
$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('error', function ($e) {
$f = fopen('push-server-error.log', "a");
fwrite($f, $e->getMessage()."\n");
fclose($f);
});
$pull->on('message', array($pusher, 'onNewEvent'));
// 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 Ratchet\Wamp\WampServer(
$pusher
)
)
),
$webSock
);
$loop->run();
?>
I successfully started the push-server.php with a monitoring tool Supervisor, I set up an NGINX proxying for WebSocket traffic, set up client side scripts ( autobahn and so on ).
In general, I was going to use it all at production. First hours I modified new chat systems on my website, I tested it and it all worked perfectly.
But I faced the problem a little bit later. Some ZeroMQ messages ( only part of them, maybe 5-10% ) are lost after sending via ZeroMQ PUSH socket. At that, this problem appears when around 300-400 messages are sent since the moment the push-server.php process was started.
I am deeply convinced that this problem is inside the ZeroMQ ( NOT inside JS client side or Pusher script with business logic ) because I tried to modify the "->on(){...}" method in the push-server.php so as to display new messages on terminal ( console ) and lost messages even does not get displayed on console, i.e. the "->on(){...}" method does not catch up them.
ZeroMQ "->send()" method always returns an empty ZeroMQ socket object, when a message was successfully sent or LOST. I checked this simply by sending chat messages on my website and by getting responses ( form submitting realized with AJAX ):
var_dump($socket->send(json_encode($sData)));
What there can be this problem and how to solve it?
Server OS: CentOS 6.9 (Final)
PHP version: 5.6.31
ZMQ extension version: 1.1.3
libzmq version: 4.2.2
I created not persistent ZMQ::Context and ZMQ::Socket and my problem was solved:
$context = new ZMQContext(1, false);
$socket = $context->getSocket(ZMQ::SOCKET_PUSH);

PHP WebSocket doesn't connect

I'm using Ratchet to create a PHP WebSocket connection. On my localhost it's working normally and as expected, but when I upload it to my server it just stops working.
I'm using an online shared hosting and this is the code on the PHP file:
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
), 8000, '0.0.0.0'
);
And this is my JS file
var socket = new WebSocket('ws://mywebsite.com:8000');
The only error I got is this:
Error in connection establishment: net::ERR_CONNECTION_REFUSED
Note: I've already ran php server.php to initiate the ws server via SSH command. I got no errors there but still can't connect. I also tried using different port but nothing seems to be working.
Why I'm trying to use WebSockets?
I have an e-commerce and I'm using the WebSockets to load/update each order without needing to refresh the page and also without using JavaScript Interval, so if an order change from 'Packing..' to 'Delivery' status, the system autoupdate without needing to refresh the page.
If there is another way around this, I'm open to ideas, as long as I can get the final result.

PHP Ratchet can't connect from other device

I'm starting using PhP Ratchet socket. Following the guides I could made a simple chat application and it's working within the same computer. Example, if I open up chrome and firefox, I can interact send and receive messages, ok.
The problem is when I try to use the chat app from another computer, but within the same internet connection, or even when I tried to test it online.
When on lan, the other computers can't connect with the socket and online no one can connect.
By looking around, I found about using '0.0.0.0' or even port 5555 to enable connections from anyone. But even when using this, I can't connect.
How can I solve this? This is my files:
server.php
<?php
use Ratchet\Server\IoServer;
use Ratchet\http\HttpServer;
use Ratchet\WebSocket\WsServer;
require __DIR__ . '/../vendor/autoload.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
), 2000, '0.0.0.0'
);
$server->run();
?>
And client js file:
var socket = new WebSocket('ws://127.0.0.1:2000');
127.0.0.1 will always point to the local device, which, on devices other than the socket server, will not point to the device that is hosting the socket server. If the device that is running the socket server is not routeable from the internet, you will not be able to connect to the socket from the internet.
You can test from other devices on your LAN by figuring out what your network IP is for the device that is running the server. Then in your client code, connect to that IP. It will probably be something close to 192.168.x.x, e.g. 192.168.1.12 (it could also be in the 10.x.x.x or 172.16.x.x address spaces). Then simply use that address to connect from your client script for testing:
var socket = new WebSocket('ws://192.168.1.12:2000');
You may still run into trouble if, for whatever reason, your network is configured to drop packets on port 2000. If so, it should be fairly easy for you to change to a different port for both your server and client.

Categories