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');
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 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
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.
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.
I have a long procedure I have written in node.js, but I'd like the PHP side of my application control kicking it off. My node is something like:
var http = require('http');
http.createServer(function (req, res) {
console.log('Got request')
try{
doProcedure()
} catch(e) {
console.log('Really bad error while trying to do the procedure:')
console.error(e.stack ? e.stack : e)
}
}).listen(8124, "127.0.0.1");
When I run this on my local machine, http://localhost:8124 will trigger things correctly. On aws, I've added the 8124 port but requests to mydomain.com:8124 aren't picked up by node.
I tried stopping httpd and then letting node listen on port 80, to rule out ports not being forwarded correctly, but it still saw nothing.
So two questions, I guess:
How to get Node listening as a daemon, so I can pass over requests? ("update user x", "update user y", "update all users", etc)
How do I ping that daemon from php to start these procedures in an AWS evironment?
Bonus question: Is there a better way I should be doing this?
Thanks all,
~Jordan
If you omit the second argument to listen(), node will listen on all IP addresses. That way you can run the same code locally to test and also on your EC2 instance.
In your catch block, you might also want to send back an HTTP error response to the client.
you should have a 10.* ip iirc for aws, your elastic/dynamic ip cant be bound to
have you tried using your server's IP address (instead of 127.0.0.1) in the createServer.listen() call?