antares667367 antares667367 - 4 months ago 17
AngularJS Question

Using in routes

I use between my angular app and my server without any trouble but I don't know how to do it when it comes to use in my routes.

Here is what i did so far :


var server = app.listen(port);
var io = require('').listen(server);
io.sockets.on('connection', function (socket) {

as i said , it works.

Now i want to use in one of my routes , routes loaded from my router :

require('./app/my.routes')(app,io);//i've tried to use it this way


module.exports = function (app,io){
io = app.get('io');//and get it this way
io.sockets.emit(' :: sucessfully passed from app to router')'Trying to load express routes...');
router.use(function (req, res, next) {
next(); // make sure we go to the next routes and don't stop here
//require routes
var consoleRoute = require('./express.routes/console.route')

and now I have that error

io.sockets.emit(' :: sucessfully passed from app to router')

TypeError: Cannot read property 'sockets' of undefined

I must do something wrong obviously but I have no idea what.
Any ideas ?

I also tried
in app.js and
on the other side but it failed all the same


Finally , i've found the solution, = io.sockets.on('connection', function (socket) {
socket.on('server', function (data) {;
socket.emit('client', {
action: "alert",
text: 'test from socket io'

Now it's linked to app, and I can use it anywhere.


You can setup your socket io implementation in your express configuration file and pass your "io" object as a variable within the app. = io;

the idea is to do something like this:

module.exports = function (app){' sucessfully passed from app to router');