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

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.

Related

WebSocket connection to 'ws://localhost:8080/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

I am getting no Websocket connection between client script and server.
It is working fine in my local environment.
Also I am following this link.
Here's a server script that initializes the websocket server and listens for the client connection to 8080 port.
public function run()
{
$loop = Factory::create();
$pusher = new Pusher;
$context = new 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 Server('0.0.0.0:8080', $loop); // Binding to 0.0.0.0 means remotes can connect
$webServer = new IoServer(
new HttpServer(
new WsServer(
new WampServer(
$pusher
)
)
),
$webSock
);
$loop->run();
And here's the client script:
var conn = new ab.Session('ws://localhost:8080',
function() {
/* subscribe to following topics */
conn.subscribe('new_order', function(topic, data) ..
Again, this is working fine in the local setup.
Also to note, my application is hosted using a specified port from docker container.
http://192.168.12.52:8094/xyz/new/....
I also tried specifying the IP in the client script:
var conn = new ab.Session('ws://192.168.12.52:8080',
function() {
/* subscribe to following topics */
conn.subscribe('new_order', function(topic, data) ...
In which case I get the following error:
WebSocket connection to 'ws://192.168.11.32:8080/' failed: Error during WebSocket handshake: Unexpected response code: 403
What is missing here?
This is probably an ssl certificate issue. I think new browsers versions have problem with web sockets without ssl. you may have self signed certificate. try https://192.168.12.52:8080 and if you saw an invalid certificate error (something like: Your connection is not secure or Your connection is not private) click advance or add exception then try again with the same browser. also you can use wss:// instead of ws://.

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);

Ratchet Websocket for raspberry pi

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.

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.

Categories