How to run PHP websocket on OVH cloud-web-1 host, - php

I have a PHP application which uses websockets,
When I run it on my local, it works fine, but when I deploy it on my web host, it doesn't anymore.
I am on cloud-web-1 of OVH.
I saw that this plan supports websockets.
When I launch the PHP server script with SSH, it works fine, but on the client side, when I create a js object socket with url of the websocket ws://ip-address:port or wss or ws://my-domain:port , it never work and it always show error on the browser console like "Connection to websoccket failed.",
Help me please,
Thanks

On the server side (your PHP code), ensure your websocket is listening on http://0.0.0.0:<port> (I tried with port 3000).
On the HTML side, here is an example of connection to your websocket:
<script src="/socket.io/socket.io.js"></script>
<script>
var host = location.protocol + '//' + location.host;
var socket = io.connect(host);
socket.on('news', function (data) {
<your code>
}
</script>

Related

Run Ratchet on a subdomain as a complete own service to provide a custom WebSocket

I'm currently trying to host my own WebSocket server using Ratchet http://socketo.me/docs/push.
The problem is that I can't find a good tutorial that shows me how I can host this on a subdomain. So hopefully someone can help me here.
My plan:
I already have a basic auth secured subdomain called ws.my-domain.de. Now I want to run Ratchet on my subdomain to provide this as a service for my main domain and all my subdomains.
At my main domain my-domain.de I've WordPress running so this is where I want to use my own WebSocket first via the client side tutorial from the page I've posted above:
<script src="https://gist.githubusercontent.com/cboden/fcae978cfc016d506639c5241f94e772/raw/e974ce895df527c83b8e010124a034cfcf6c9f4b/autobahn.js"></script>
<script>
var conn = new ab.Session('ws://ws.my-domain.de',
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>
So can please someone show me the steps I need to do? I'm completely new with this. I know how to use it on the client side, but I don't know how to provide it as a service and then use it in PHP (WordPress).
I'm assuming you are on a VPS or dedicated server.
You need to run your WebSocket as a daemon and then create a vhost for your subdomain in Nginx or Apache and using that create a reverse proxy to your WebSocket server.
Running ratchet as a daemon
Nginx WebSocket reverse proxy
Apache WebSocket reverse proxy

Connection to websocket server : OK from javascript, not OK from php

I am using socketio to run a websocket server.
var fs = require('fs');
var options = {
key: fs.readFileSync('/etc/letsencrypt/live/my.domain.be/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/my.domain.be/fullchain.pem')
};
var app = require('https').createServer(options)
, io = require('socket.io').listen(app)
app.listen(6666);
Connecting to it from javascript (using socket.io client) works fine.
var socket = io.connect('wss://my.domain.be:6666');
This works also :
var socket = io.connect('https://my.domain.be:6666');
Now I want to connect using php.
I found a simple php client here : How can I send data/text from PHP using WebSocket to process?
But I see no incoming connection when using :
$WebSocketClient = new WebsocketClient('wss://my.domain.be', 6666);
$WebSocketClient->sendData("MyUserNameFromPHP");
And I get the error :
Error: 22145040:Unable to find the socket transport "wss" - did you
forget to enable it when you configured PHP?
When using :
$WebSocketClient = new WebsocketClient('https://my.domain.be', 6666);
I get the error :
Error: 10905616:Unable to find the socket transport "https" - did you
forget to enable it when you configured PHP?
When using tls:// or ssl:// I get no output at all (and still no connection on my websocket server).
So what am I missing to make a websocket connection from my php code ?
https://stackoverflow.com/a/61808878/3701985
please try my answer on another similar thread, I actually tried multiple solutions and now just want to provide answer which worked for me.
https://github.com/ratchetphp/Pawl

socket.io polling returns Laravel 500 NotFoundHttpException

I'm trying to setup a node.js socket.io server, and it's not working. It seems like it's trying to poll to the node.js server first, but because I'm using Laravel for the majority of my app, that's picking it up and returning a Not FoundHttpException
Node.js server code:
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var redis = require('ioredis');
var Redis = new redis();
Redis.subscribe('live-updates');
Redis.on('message', function(channel, message) {
message = JSON.parse(message);
console.log(channel, message);
io.emit('foo', message.data);
});
io.on('connection', function() {
console.log('foo');
});
Client code:
var socket = io.connect('http://myapp.app');
socket.on('foo', function(data) {
console.log(data);
});
All this does is triggers the following HTTP long polling requests in my client:
http://myapp.app/socket.io/?EIO=3&transport=polling&t=1447468676627-0
which responds with:
Server 500 error, #29 Symfony\Component\HttpKernel\Exception\NotFoundHttpException in /home/vagrant/myapp/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:161
Well of course it's not found. I'm expecting socket.io to attempt to create a websocket connection - not send long-polling HTTP requests to an undefined Laravel route. What am I doing wrong?
You don't actually need express here, as you are not displaying anything, just simple http server.
I think your issue, that the server does not listen.
Also node should listen to different port than 80, otherwise your http server will handle the request and lead to that 500 error.
Check code below.
Server side
var app = require('http').createServer(handler);
var io = require('socket.io')(app);
app.listen(3000);
Client side
var socket = io('http://localhost:3000');
Make sure you run node from CLI
node index.js
And replace index.js with your script name.

Javascript connect to websocket

Following up form my last question -
var socket;
if ("WebSocket" in window)
{
alert("WebSocket is supported by your Browser!");
// Let us open a web socket
socket = new WebSocket("ws://localhost:10001");
}
socket.onopen() = function(){
alert("Connection Opened");
}
socket.onmessage() = function(msg){
alert(msg);
}
I can connect to the server with telnet but I can't seem to connect using Javascript, why is this?
Because WebSocket is not a normal, general-purpose socket. It requires the server on the remote end to conform to a very specific handshake defined by the WebSocket protocol. If your server does not implement this protocol, WebSocket cannot connect to it.
Additionally, as Rocket points out, your code is currently attempting to call socket.onopen() and assign a value to the function call. Lose the parentheses.

Trigger a nodeJS socket.io server to broadcast via port 80

Realtime Update Mechanism : user writes -> php save it in mysql db -> php send info to nodeJS -> nodeJS send the change all subscribers -> others can notice it in realtime.
The Socket.io server works well and runs on port 8080. I have node http server running on port 80. How can I get a trigger message on the http server and send the message to all socket.io clients?
var io = require('socket.io').listen(8080);
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Message Broadcast\n');
console.log("Message Sent");
//DOES NOT SEEM TO WORK, error
io.broadcast("Messages");
//No error but no messages
io.emit("Message");
}).listen(80, "0.0.0.0");
/**
Other socket.io code which works well..
....
In Socket.IO 0.7 you should use the following instead to send to all connected sockets without a specific socket reference:
io.sockets.send("Messages")
Note that this is different to broadcast since that would send to all sockets except the socket that initiated the connection - of course you require a sending socket reference which is why your current code wouldn't suffice.
Quote from the wiki page: Socket.IO/Wiki
If you want to send a message to everyone you can reference io.sockets:
io.sockets.send('message');
io.sockets.emit('event');

Categories