I am trying to run nodejs on php file.
<script src="htps://xxx.com:8445/socket.io/socket.io.js"></script>
<script src="htps://xxx.com:8445/easyrtc/easyrtc.js" type="text/javascript"></script>
Server.js :
// Load required modules
var https = require("https"); // https server core module
var fs = require("fs"); // file system core module
var express = require("express"); // web framework external module
var io = require("socket.io"); // web socket external module
var easyrtc = require("../"); // EasyRTC external module
// Setup and configure Express http server. Expect a subfolder called "static" to be the web root.
var httpApp = express();
httpApp.use(express.static(__dirname + ":8445"));
// Start Express https server on port 8445
var webServer = https.createServer(
{
key: fs.readFileSync("/etc/apache2/ssl/xxx.com.key"),
cert: fs.readFileSync("/etc/apache2/ssl/xxx.com.crt")
},
httpApp).listen(8445);
// Start Socket.io so it attaches itself to Express server
var socketServer = io.listen(webServer, {"log level":1});
// Start EasyRTC server
var rtc = easyrtc.listen(httpApp, socketServer);
ERROR :
Nodejs works but i got an error like that. If i didnt use PHP, it can work.
For anyone coming from google with this problem, easyRTC was assuming the http server and the ws server was on the same url/port. To fix the issue the method easyrtc.setSocketUrl("https://thedomain.com.tr:8445"); was required to run after including easyRTC
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 want to use socket.io in codeigniter and I have followed all the process like install express and socket.io in root and I also have created the server.js file bellow
server.js
var socket = require('socket.io');
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = socket.listen(server);
var port = process.env.PORT || 3000;
server.listen(port, function() {
console.log('Server listening at port %d', port);
});
io.on('connection', function(socket) {
socket.on('new_message', function(data) {
io.sockets.emit('new_message', {
message: data.message,
date: data.date,
msgcount: data.msgcount
});
});
});
When I run this server.js in terminal, it outputs 'Server listening at port 3000',
but when I request localhost:3000 in browser, it shows Cannot GET /. I do not understand this error.
You are adressing http request to express router which is not set in your code. Socket.io is sending xhr or socket requests and does not handle http requests. Either use online services like Socket echo or raise router which will receive your browser request and echo it it via socket to your socket.io instance.
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
We have existing laravel application running on VPS provider on ubuntu with apache, we have to implement real-time features through node app (socket.io and express) seems fine in local environment but when i try it to production it doesnt working at all, my question is, what is the proper configuration i will be made in order to run my node app like $ node server.js
snippet code(server.js).
var socket = require('socket.io');
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = socket.listen(server);
var port = process.env.PORT || 3001;
server.listen(port, function () {
console.log('Server listening at port %d', port);
});
io.on('connection', function(socket) {
code here....
});
snippet code(client-side(js-file)).
var socket = io();
ERROR
GET https://hostname/socket.io/?EIO=3&transport=polling&t=1480917572919-5 404 (Not Found)
snippet code(client-side(js-file)).
var socket = io() ;
to
var socket = io.connect('http://localhost:3001');
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.