Error sharing HTTP Session with socket.io. io.sets "is not Valid".

Asked

Viewed 167 times

1

I am applying the step by step book "Node.js, real-time applications" of Caio Ribeiro Pereira. All right until the part about sharing the HTTP express-Session to the socket.io.

As far as I can see there’s nothing wrong, but when I try to turn on the server, this appears:

inserir a descrição da imagem aqui

The server turns on and navigates the application but drops as soon as I try to enter the chat route.

inserir a descrição da imagem aqui

Here’s my code for the first image error

io.set(function(socket, next) {
    var data = socket.request;
    cookieSec(data, {}, function(err) {
        var sessionID = data.signedCookies[KEY];
        SesMemStore.get(sessionID, function(err, session) {
            if (err || !session) {
                return next(new Error('acesso negado'));
            } else {
                socket.handshake.session = session;
                return next();
            }
        });
    });
});

It would be some express or socket update problem.io?

  • 1

    As for the second error, you have tried to send the headers after the content, which is impossible by HTTP. As for the socket.io / express let us know what version it is using so we can know.

  • ^1.2.1 is the version of my socket.io. As for the late header, why this is occurring since it is a process that is not normally manipulated?

  • @Olimonf. I forgot to mark you

1 answer

1

I believe this API of socket.io is deprecated. The solution would be to change its use of the method Server.prototype.set for Server.prototype.use, that will plant a middleware like those of the express.

There is an updated version of the examples of Caio Ribeiro Pereira’s book. I think that that example demonstrates what you are trying to do by using Server.prototype.use:

I found an article from socket.io explaining the differences since version 0.9, specifically with regard to authentication, I suggest you take a look at here.

Your code should work with this little change of set for use:

io.use(function(socket, next) {
  var data = socket.request;
  cookieSec(data, {}, function(err) {
    var sessionID = data.signedCookies[KEY];
    SesMemStore.get(sessionID, function(err, session) {
        if (err || !session) {
            return next(new Error('acesso negado'));
        } else {
            socket.handshake.session = session;
            return next();
        }
    });
  });
});

Of the documentation of socket.io, freely translated into English:

Namespace#use(fn:Function):Namespace

Logs a middleware; a function that runs for every connection (Socket) that arrives and receives as parameters the socket and a function to optionally pass to execute the next middleware registered.

Here, Namespace#use(fn:Function):Namespace, means that this is a method about the class Namespace, which takes a function as argument and returns a Namespace, for chaining. It is the default to return the this to be able to chain methods (another example would be the superagent: request.get(url).query(query).end(fn);).

This may seem confusing, because the object io in your code is an instance of the class Server, not of class Namespace. The idea is that you can create multiple objects Namespace, exactly how you can create multiple objects Router in the express 4, each with its own set of handlers and middleware.

By default, Server.prototype.use will call Namespace.prototype.use about the Namespace standard, which handles the connections to /. This is similar to the relationship between the Application and the Router of express.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.