'How to define a route when using Ratchet web socket?
I am new to this field regarding web socket. I am trying to create a simple real time application built on PHP. I found that Ratchet is one of the best choice for PHP as it eases for native PHP socket programming. I followed the 'Hello world' tutorial found in Ratchet official site hello world. This tutorial is basic and it just shows how to make the entire tutorial through web-browser console.
As per the tutorial it has provided a simple shell script that we need to execute so that Ratchet web socket runs as a service alongside Apache server. Below is the Shell script:
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
require dirname(__DIR__) . '/vendor/autoload.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8080
);
$server->run();
In the above code, it says that web socket is accessible via port 8080. I went to browser and browser for localhost:8080 server is running without any error and browsing localhost:8080 in the browser is displayed without any error. I guess everything is going well.
The next thing is to establish a connection from the client side which can be done by supplying javascript code in the browser console. (Below is the javascript code)
var conn = new WebSocket('ws://localhost:8080');
conn.onopen = function(e) {
console.log("Connection established!");
};
conn.onmessage = function(e) {
console.log(e.data);
};
When I run the above code, connection is established without any error. Now the problem is I do not know where to put the above JavaScript code so that I do not have to write in console everytime. I guess it should be saved in some javascript files. The above code works in port localhost:8080. But how can I mention explicitly that the link localhost:8080/chat.php where chat.php would be the desired page that will be shown in the browser and maybe I can put the above JavaScript code in the same file.
I do not know If my question is valid or not, I am just too beginner in this field. I have also asked another question Binding ZMQ Socket and Ratchet regarding the same subject but have not been satisfied yet .
Solution 1:[1]
In order to use routes you need to make use of the Symphony Routing functionality built into Ratchet.
Basically your socket initialisation would look like this
$app = new Ratchet\App("localhost", 8080, '0.0.0.0', $loop);
$app->route('/chat', new Chat, array('*'));
$app->run();
This tutorial goes into more detail and explains some more simple tricks with PHP WebSockets
https://blog.samuelattard.com/the-tutorial-for-php-websockets-that-i-wish-had-existed/
Solution 2:[2]
I follow the response of MarshallOfSound I have found some way to use the routing with the new version of Ratchet
$app = new Ratchet\App('127.0.0.1',8080);
$app->route('/chat', new Chat(), ['*']);
$app->run();
This code work, using the router with the HTTPServer object.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MarshallOfSound |
| Solution 2 |
