How to access io instance in a third part file? - node.js

I need to access my socket.io instance in some differents files, how do you make it works?
Here is what i tried:
main.js
var app = express();
var sockets = require('./sockets');
sockets.listen(app)
sockets.js
var io = require('socket.io');
exports.listen = function(app) {
io = io.listen(app);
//...
}
exports.io = io;
SomeClass.js
var io = require('./sockets').io;
var SomeClass = function() {
var Clients = io.sockets.clients('room');
//io is undefined...
}
exports.SomeClass = SomeClass;

In your file, io.sockets is undefined because you are starting your server incorrectly. Socket.IO expects a HTTP server instance, and you are instead passing an Express instance. You are also trying to change a variable after it has been exported, and that does not work. This is what you should be doing:
var http = require('http');
var express = require('express');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server):
server.listen(80);
If you still wanted to split this up into files, you would have to put your Socket.IO instance in your main application, and pass it to other modules when requiring them.

Related

Separating modules in NodeJS for use with a single object

I have a NodeJS + ExpressJS + Socket.IO server and I am trying to divide my different namespaces into different modules.
Essentially I want to require the socket.io library in server.js, but have access to the io variable from my modules like this:
server.js
var app = require('express')();
var jwt = require('jsonwebtoken');
var server = require('http').Server(app);
var fs = require('fs');
var io = require('socket.io')(server);
var bodyParser = require('body-parser');
var _log = require('./logging/loggly.js').Client();
// Global NS
var SK = {
Namespaces:{}
};
var Migrations = require('./sockets/migrations');
var Backups = require('./sockets/backups');
var Cloudmanager = require('./sockets/cloudmanager');
SK.Namespaces[Migrations.NS] = Migrations;
SK.Namespaces[Backups.NS] = Backups;
SK.Namespaces[Cloudmanager.NS] = Cloudmanager;
....
....
migrations.js
var exports = module.exports = {};
///////////////////////////////////////////////
//
// Migrations Namespace
//
///////////////////////////////////////////////
exports.NS = 'migrations';
exports.socket = io.of('/'+exports.NS); // PROBLEM IS 'io' IS UNDEFINED HERE
exports.socket.on('connection', function(socket){
});
I have basically the same code in all 3 of my socket.io namespaces, but I don't have access to the io variable that is used in server.js. Is there a way to have access here? Do I just use a require()? Whats the best way to achieve this functionality?
Thank you
You can export a function from migrations.js that accepts the io value as a parameter:
module.exports = function (io) {
var socket = io.of('/'+exports.NS);
socket.on('connection', function(socket){});
return {
NS: 'migrations',
socket: socket,
};
};
Then simply require and invoke this function in your server.js:
var Migrations = require('./sockets/migrations')(io);

Websocket With Socket.io & Angular

I want to create a websocket to add a communication between my angular app and my database. The app shall be able to save a question in a database & to notify the user when somebody answered.
Unfortunately I tried some tutorials which all don't work. I'm totally new to this because I use apache usually. So If you know a "working", very basic (beginner) tutorial, which doesn't require to install lots of additional things like yeoman etc. I would be glad.
In the current tutorial I get the error message:
You are trying to attach socket.io to an express request handler function. Please pass a http.Server instance.
My server.js:
var connect = require('connect'),
serveStatic = require('serve-static'),
socket = require('socket.io');
var server = connect();
server.use(serveStatic(__dirname+'/../client'));
server.listen(8080);
var io = socket.listen(server);
console.log("Server started and listen to http://127.0.0.1:8080");
without
var io = socket.listen(server);
it serves my static page.
This error is on my other approach:
has no method 'use'
My server.js
var connect = require('connect');
var http = require('http');
var serveStatic = require('serve-static');
var app = connect();
var server = http.createServer(app).use(serveStatic(__dirname+'/../client')).listen(3000);
var socket = require('socket.io');
var io = socket.listen(server);
As always if you struggle long the solution comes quick after posting. That one works for the beginning:
var connect = require('connect');
var http = require('http');
var app = connect();
var fs = require('fs');
var index = fs.readFileSync(__dirname+'/../client/index.html');
var server = http.createServer(function(req, res){
// Send HTML headers and message
res.writeHead(200,{ 'Content-Type': 'text/html' });
res.end(index);
}).listen(3000);
var socket = require('socket.io');
var io = socket.listen(server);

Cannot call method "on" of undefined (express and socket.io)

I want to export the socket.io object from my app.js of express
My app.js file is here: http://pastebin.com/Kfny4yVK
My bin/www file is here: http://pastebin.com/qGhPm6KE
In my app.js I do:
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
exports.io = io;
In a routes file I do:
var io = require(__dirname+'/../app.js').io;
However, when I call the:
io.on
I get an undefined object error. Any idea why this is happening? It looks like nodejs is somehow modifying the io object at export? Is this possible? Is there a way to make this work?
With Express v4.x I use the following to export the socket.io object.
APP.JS
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
//Export sio module so socket.io can be used in other modules
module.exports.sio = io; //ADDED this
OTHER_FILE.JS
//Import sio module from app.js
var io = require('../app.js').sio;

Sharing variables across files in node.js without using global variables

I'm trying to separate my socket.io code into a separate file (socket.js) from my main file (app.js). However, I need to define my io object in app.js, which is also used in my socket.js file.
Currently, I set io as a global variable so it is accessible from app.js (Global Variable in app.js accessible in routes?), but I understand this is bad practise. Is there a better way to do this (can injection work in this case, as I need to export a variable from the app.js to socket.js rather than the other way round)? Thank you!
app.js
var app = express(),
server = require('http').createServer(app);
//KIV -> io is set as a global variable
io = require('socket.io').listen(server);
require('./socket');
socket.js
io.sockets.on('connection', function(socket) {
//xxx
}
One of the ways is by passing the object as argument to function (as already has been described in #Thomas' answer).
Other way is to create a new file say 'global.js'. put only those items in this file that you want to be global. e.g.
var Global = {
io : { }
};
module.exports = Global;
Now, in your app.js,
var app = express(),
server = require('http').createServer(app),
global = require('./global.js');
global.io = require('socket.io').listen(server);
require('./socket');
And in your socket.js:
var global = require('./global.js');
global.io.sockets.on('connection', function(socket) {
//xxx
}
Hope it helps...
app.js
var app = express(),
server = require('http').createServer(app),
socket = require('./socket');
var io = require('socket.io').listen(server);
socket(io)
socket.js
module.exports = function (io) {
io.sockets.on('connection', function(socket) {
// Your code here
});
}

Connect2 and Socket.io

I'm trying to get connect and socket.io to work together nicely and simply. I have the following code on server side:
var connect = require('connect'),
io = require('socket.io');
var app = connect().use(connect.logger('dev'));
var sio = io.listen(app);
app.listen(8000);
when i open http://localhost:8000/socket.io/socket.io.js i'm get error:
Cannot GET /socket.io/socket.io.js
And Socket.IO not work, i'm trying copy file and load from another location, but socket.io requests do not reach the server
SOLUTION
if anyone comes to this issue, you need to wrap the connect/express app in a node http.Server. The app.listen() method is a convenience method for this and returns the server:
var io = require('socket.io');
var app = connect();
var server = app.listen(3000);
io.listen(server);
or the following is equivalent:
var io = require('socket.io');
var http = require('http');
var app = connect();
var server = http.createServer(app);
server.listen(3000);
io.listen(server);

Resources