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
Related
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>
I have made a chat with SocketIO who is working well on local, but i'm trying to deploy it on my Apache server.
I'm using Php + NodeJs, not only Node
I had ERR_CONNECTION_REFUSED error but solved it by opening the good port, Listen 8000 in my ports.conf
My server.js look like this
var io = require('socket.io').listen(8000);
// Socket IO usage
My client.js is
var socket = io.connect('http://[MY SERVER IP]:8000');
// Other client code
I use localhost in local but I changed by my server ip.
But I still have this error
XMLHttpRequest cannot load http://[MY_SERVER_IP]:8000/socket.io/?EIO=3&transport=polling&t=1455101301883-60. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'MY DOMAIN' is therefore not allowed access. The response had HTTP status code 404.
I really don't know what to do, this is the first I try to deploy a Nodejs + php app.
I had read some Stackoverflow questions to try to fix it, but i really don't know how...
I don't knwo if this is due to my Apache conf or I must change some NodeJS or SocketIO conf
Thanks for your help.
I found the problem.
Do not open the NodeJS server port on Apache config. It will make some conflicts and will prevent your Node server to work.
Just change the localhost by your server IP and it will work fine.
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.
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');
Hi
My proplem is that I have a open socket in my server side code (written php) and I need to trasform this socket to use TLS, with negotiation and decription of the stream before passing it to the already existing code that work with a clear stream without encryption.
What is the simplest solution to archive this?
PHP supports TLS/SSL listening using a stream context. Something like this should work:
$listenstr = "tls://0.0.0.0:1234";
$ctx=stream_context_create(array('ssl'=>array(
"local_cert"=>"mycertandkey.pem",
"passphrase"=>"badpassphrase"
)));
$s = stream_socket_server($listenstr,$errno,$errstr,STREAM_SERVER_BIND|STREAM_SERVER_LISTEN,$ctx);
$conn = stream_socket_accept($s);
// do stuff with your new connection here