antares667367 antares667367 - 2 years ago 97
AngularJS Question

SOLVED - 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 i want provided i pass the app var.

Answer Source

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');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download