socket.io can't connect to server - node.js

I'm hosting my application on openshift. I am using a custom domain. And socket.io wasn't able to download the client side script so I just used the cdn instead. But now it's not able to connect to a namespace. These are the errors it is giving me on the console log
This is my client side code on the .html page to download the client side script
<script src="https://cdn.socket.io/socket.io-1.3.7.js"></script>
and the .js index page to connect to the index namespace
var socket = io("http://www.loomius.com/index");
Here is my server side code
var express = require('express');
var app = express();
var http = require('http');
var io = require('socket.io')(http);
var mongoose = require('mongoose');
var favicon = require('serve-favicon');
var bodyParser = require('body-parser');
var https = require('https');
// listening on the port
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || 3002);
app.set('ip', process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1");
http.createServer(app).listen(app.get('port') ,app.get('ip'), function () {
console.log("✔ Express server listening at %s:%d ", app.get('ip'),app.get('port'));
});

First instead of
var socket = io("http://www.loomius.com/index");
use this to solve issue with a need of cdn
var socket = io.connect("/");
Then on server use this instead
http = http.createServer( app ).listen( process.env.PORT, process.env.IP || "0.0.0.0", function() { // or define ip and port manually
var io = require( 'socket.io' )( http );
io.on('connection', function( socket ) {
// add event listeners here
}
});

Related

Nodejs socket.io client cannot connect to Nodejs socket.io server

I use NodeJS both as the server and the client. (No web browsers)
Server seems to be working, but client does not connect. I tried to set the port in the options but it did not work. I try to connect to the port 3000 over telnet and it connects to something, so the server is listening.
What am I missing here?
Server:
var express = require('express');
var app = express();
var path = require('path');
var server = require('http').createServer(app);
var io = require('socket.io')();
var port = 3000;
server.listen(port,"127.0.0.1", () => {
console.log('Server listening at port %d', port);
});
Client:
const io = require('socket.io-client');
const socket = io("http://127.0.0.1:3000",{reconnect:false});
socket.on('connect', function () {
console.log('connected to the server');
});
As mentioned on the offical socket-io github page, you need to pass the http-server instance to the socket-io server:
...
var server = require('http').createServer(app);
var io = require('socket.io')(server);
...

Error EADDRINUSE when requiring socket.io server through multiple files

I'm trying to export socket.io server through multiple node script so i can emit notification on the same port.
Here is my main server.js file code:
var express = require('express'),
app = module.exports.app = express();
const options = {};
var server = http.createServer(app);
var io = require('socket.io').listen(server);
exports.io = io;
server.listen(3000, function() {
console.log('Node.js Global app is running...');
});
Below is other node script are runninig when i try to require server.js i get this error:
Error: listen EADDRINUSE 0.0.0.0:3000
server_tn.js
var express = require('express'),
app = module.exports.app = express();
var code_pays = path.basename(__dirname);
console.log('Node.js app is running...' + code_pays);
var main = require('./../main.js');
var importIo = require('./../server');
var io = importIo.io;
main.mainTraitement(code_pays);
You can't have more than one program listenning to a specific port.
Check if you have any program listening on port 3000, or if on your main.js you are also listenning on port 3000.

How to start node express, binaryserver and socket.io on same port?

I have code snippet to explain what i am doing and what i want.
var express = require('express');
var http = require('http');
var app = express();
app.use('/', express.static(__dirname + '/static'));
var BinaryServer = require('binaryjs').BinaryServer;
var server = http.createServer(app);
var binaryServer = new BinaryServer({server:server});
var ioServer = http.createServer(app);
var io = require('socket.io').listen(ioServer);
I can run node express and socket.io on same port.
ioServer.listen(8080, function(){
console.log('server running at localhost:8080');
});
Same can be done with node express and binaryServer.
server.listen(8080, function(){
console.log('server running at localhost:8080');
});
But i want to run node express, socket.io and binaryServer on same port express is running (8080 in this case).
Any suggestions ?
You would need to attach both the SocketIO and binaryServer to same http server instance then bring that single instance up.
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var binaryServer = new BinaryServer({ server:server, path: '/binary'});
server.listen(8080, function(){
console.log('http/socket/binary server running at localhost:8080');
});
Set the path so binaryServer doesn't conflict with any of your apps. This path is required in the client connections too.

node JS server connection

I'm trying to use the code below but it doesn't work for me because the server doesn't connect and there's no error in the console.
var PORT=8080;
var http=require('http');
var http=require('swig');
var http=require('url');
exports.startServer=function (PORT){
var server=http.createServer(function(req,res){
var page=url.parse(req.url).pathname;
if(page=='/'){
res.writeHead(200,{'Content-Type':'text/html'});
res.write(swig.renderFile('templates/home.tpl',{
name :'user'
})
);
} else{
res.writeHead(404,{'Content-Type':'text/html'});
res.writeHead('<h1>Error 404 : page not found</h1>');
}
res.end();
});
server.listen(PORT);
console.log('Server running on'+ PORT);
}
The reason I think is that you have assigned the variable http for three times, which makes that only the last one takes effect.
Try to replace
var http=require('http');
var http=require('swig');
var http=require('url');
with
var http=require('http');
var swig=require('swig');
var url=require('url');
check any other server running on same port or not
if it is running, change port number
var port = process.env.PORT | 3000;
change this
var http=require('http');
var swig=require('swig');
var url=require('url');
Better to use express.
var express = require('express');
var app = express();
var port = process.env.PORT | 3000;
var server = app.listen(port,'0.0.0.0',function(){
var host = server.address().address;
var port = server.address().port;
console.log("Server listen to : "+host+":"+port);
});

Node + SSL = SLOW

I have set up a node.js-server for running a chat service used by our site. It works. However some customers are NOT able to connect (is done automatically via javascript). They never pop up in the list of connected users (in ie 7,8,9).
My site is running on https (port 443) and therefore my node.js-server is also running on SSL (port 8443) (with the same certificate).
I am using Node 0.6.20 (have tried numerous other versions). I have the following package-setup:
{
"name":"My Chat",
"description":"Chat app using socket.io",
"version":"0.0.1",
"dependencies":{
"express":"3.x.x",
"socket.io":"~0.8.7"
},
"engines":{"node":"0.6.20"}
}
and my node server looks like this:
var fs = require('fs');
var express = require('express');
var https = require('https');
var sio = require('socket.io');
var https_options = {
pfx: fs.readFileSync('cert/certificate.pfx'),
passphrase: "password"
};
var PORT = 8443;
var HOST = '0.0.0.0';
var myArray = new Object();
var userArray = {};
var nicknames = {};
app = express();
app.use(app.router);
server = https.createServer(https_options, app).listen(PORT, HOST);
console.log('HTTPS Server listening on %s:%s', HOST, PORT);
var io = sio.listen(server);
io.set("transports", [ 'websocket'
, 'flashsocket'
, 'htmlfile'
, 'polling'
, 'xhr-polling'
, 'jsonp-polling']);
// routes
app.get('/hey', function(req, res) {
res.send('HEY!');
});
app.post('/ho', function(req, res) {
res.send('HO!');
});
io.sockets.on('connection', function (socket) {
});

Resources