2

How to use socket.io in Mean.io stack?

First of all, Mean.io changes their folder structure very regularly.. So my question is where is the best place to configure socket.io ? or is it better to use express.io ?

Second I am still not quite sure where to look for to find code that tells mean.io to listen for port, I have found a port is defined in config folder in all.js file, but real problem is as soon as I define server.listen(port) app doesn't load. and if I don't app loads but socket.io doesn't work.

Also I have another question about /socket.io/socket-io.js file? I have copied that in index folder, but my app can't find it or says 404 error. I know it's not an actual file sitting on any such location as far as I have understood, also people suggested to put that line as 127.0.0.1/socket.io/socket-io.js but none made the js file available for the app to be able to run socket.io.

What is the proper way of defining socket.io in mean.io framework?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rumman Ahmed
  • 189
  • 1
  • 1
  • 11

2 Answers2

3

I also faced the same issue and took me about a week to finally get it right. I'll try to explain what I did:

app.js

In this file, I just invoke the code that creates and sets up a socket.io object for me, which is then passed to the routes module.

'use strict';

/*
 * Defining the Package
 */
var Module = require('meanio').Module;

var MeanSocket = new Module('chat');

/*
 * All MEAN packages require registration
 * Dependency injection is used to define required modules
 */
MeanSocket.register(function(app, http) {

    var io = require('./server/config/socketio')(http);

    //We enable routing. By default the Package Object is passed to the routes
    MeanSocket.routes(io);

    return MeanSocket;
});

server/config/socketio.js

This file simply configures the socket.io object. Please note that I had to upgrade meanio module to version 0.5.26 for this work, as http object (express server) is not available in older meanio versions. Moreover, in case you want to use ssl, you can inject https instead of http.

'use strict';

var config = require('meanio').loadConfig(),
    cookie = require('cookie'),
    cookieParser = require('cookie-parser'),
    socketio = require('socket.io');

module.exports = function(http) {

    var io = socketio.listen(http);

    io.use(function(socket, next) {
        var data = socket.request;

        if (!data.headers.cookie) {
            return next(new Error('No cookie transmitted.'));
        }

        var parsedCookie = cookie.parse(data.headers.cookie);
        var sessionID = parsedCookie[config.sessionName];
        var parsedSessionID = cookieParser.signedCookie(parsedCookie[config.sessionName], config.sessionSecret);

        if (sessionID === parsedSessionID) {
            return next(new Error('Cookie is invalid.'));
        }

        next();
    });

    return io;
};

routes/chat.js

Finally, use the routes file to define the socket events, etc.

'use strict';

// The Package is passed automatically as first parameter
module.exports = function(MeanSocket, io) {

    io.on('connection', function(socket) {

        console.log('Client Connected');

        socket.on('authenticate', function(data, callback) {

        });
    });
};

Hope this helps!

Digitrance
  • 759
  • 7
  • 17
  • It has helped me a lot! The only solution that I found that keeps mean.io "packageination", without editing other modules, and very easy to implement. I understand that this solution will work (this 'chat' module) with future version of mean.io as they keep available http object of meanio module. Thanks a lot!! – adri14 Nov 01 '14 at 11:56
  • @adri14 I'm glad this helped you. Yes, this solution is non-invasive, and should work in future versions of mean.io as long as http object is available. – Digitrance Nov 02 '14 at 16:41
  • 1
    Just wanted to add that this code has finally made into the official mean-socket implementation. So, do check it out! And it also resolves the issue of deploying meanio and mean-socket behind a single port, which was not supported earlier. – Digitrance Nov 06 '14 at 17:19
1

The simplest way would be to install the socket package...

mean install socket
Lior Kesos
  • 294
  • 1
  • 5
  • 1
    I tried this but it crashes the app. `..../iso/node_modules/cookie-parser/lib/parse.js:44 return str.substr(0, 2) === 's:' TypeError: Cannot read property 'substr' of undefined` – martyn May 30 '15 at 11:14
  • 1
    I installed socket module and it broke the cookie-parser. I started a discussion for this : http://stackoverflow.com/questions/31103128/installing-socket-module-in-mean-io-broke-cookie-parser – user583726 Jun 28 '15 at 18:09