Motivation:
I want to update my front-end directly from the server.
I can't connect the client to the server. (Error on client)
Socket.io v4
Node.JS server runs on port 3000
Client on port 80
Server side
var express = require('express');
const { Server } = require("socket.io")
const http = require('http');
const Sockets = require('./sockets');
var app = express();
const miserver = http.createServer(app);
app.use(express.static(__dirname + '/public'));
const httpserver = miserver.listen(3000);
console.log("Servidor corriendo en el puerto 3000");
//incluir socket IO
const el_io = new Server(httpserver, {
cors: {
origin: "http://localhost:80",
methods: ['POST', 'GET'],
credentials: true
}
});
Sockets(el_io);
Client side
<script src="../socket.io.js"></script>
<script>
var socketIO = io('ws://localhost:3000/socket.io/?EIO=3&transport=websocket');
</script>
the socket run at port 80
Nothing in this code you show is running on port 80. You show the creation of ONE http server that is running on port 3000.
Also, this:
io('ws://localhost:3000/socket.io/?EIO=3&transport=websocket');
is wrong. The path and parameters are things that socket.io adds to the URL itself. You should just be doing this:
const socketIO = io('http://localhost:3000');
and, if you want to avoid CORs issues, you can tell socket.io to go directly to a webSocket like this:
const socketIO = io('http://localhost:3000', {transports: ['websocket']});
Related
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 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
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 am trying to create a port over at localhost:3000 but it doesn't work. If I visit localhost:3000 from my browser, I get a 404.
I know the problem. I haven't even opened a port. I just randomly put that port number 3000 there. I don't even know.
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_count_message', function( data ) {
io.sockets.emit( 'new_count_message', {
new_count_message: data.new_count_message
});
});
socket.on( 'update_count_message', function( data ) {
io.sockets.emit( 'update_count_message', {
update_count_message: data.update_count_message
});
});
socket.on( 'new_message', function( data ) {
io.sockets.emit( 'new_message', {
name: data.name,
email: data.email,
subject: data.subject,
created_at: data.created_at,
id: data.id
});
});
});
I honestly don't know how to open a port with MAMP. Please walk me through guys. I have spent the past 3 hours researching on web sockets. All I get it how to set them them up in PHP, Java, etc, but they don't say how to actually create the port.
Any help is highly appreciated. Thanks!
You don't actually create a port using MAMP.
So in your case, var port = process.env.PORT || 3000; you are listening to port 3000.
If you visit localhost:3000 you will get a 404. The code above, you could save that to your root directory of your server in a js file. In this e.g., let's call it server.js. After you save it, all you need to do is go to your terminal, and type cd to your directory, and then type in node server.js. In your case, you should get a message saying Server listening at port 3000.
Now, if you visit localhost:3000, you should be able to see the page.
Hope that helps!
If you're using MAMP Pro, then you can change the default Apache port for HTTP connection or https connection from the General options. However, if you're using MAMP (free), then you can modify the ports in the application's Preferences Panel.
You can find more about how to change the default port on MAMP from the official documentation: https://www.mamp.info/en/documentation/
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.