GithubHelp home page GithubHelp logo

Comments (3)

jondubois avatar jondubois commented on May 12, 2024 1

@joinfok You found a bug in socketcluster-server (the session object wasn't being added to the request). It's been fixed in v0.9.43 (https://www.npmjs.org/package/socketcluster-server). Thanks!

One way to fix it is by reinstalling socketcluster using npm.

npm remove -g socketcluster

Then make sure you run:

npm cache clean

before you install again. This should fetch the latest version of socketcluster-server which has the fix.

from socketcluster.

jondubois avatar jondubois commented on May 12, 2024

You can add middleware to intercept (or authorize) publish, subscribe and emit actions.
Middleware functions are useful to filter inbound communications.
I.e. Client => server

You can read up on the different kinds of middleware lines here: http://socketcluster.io/#!/docs/middleware-and-authorization

For example, for subscribe (to control who can see what channels), the client-side code might look like this:

// Client

socket.subscribe('foo');

The middleware might be:

// Server (worker.js)

wsServer.addMiddleware(wsServer.MIDDLEWARE_SUBSCRIBE,
  function (socket, channel, next) {
   // Get the list of channels which this session/socket is allowed to access
   // Alternatively, this could come from a database instead of socket.session
   socket.session.get('allowedChannels', function (err, channelPermissions) {
     if (channelPermissions[channel]) {
       // Allow the socket to subscribe to this channel - After calling next(), you don't need to
       // do anything else - SC will handle the rest
       next();
     } else {
       // Here we block every other channel which is not in that session's channelPermissions object
       // By passing an error to next(err), we are blocking the subscription
       next(socket.id + ' is not allowed to subscribe to event ' + event);
    }
   });
  }
);

from socketcluster.

joinfok avatar joinfok commented on May 12, 2024

I try this:

scServer.addMiddleware(scServer.MIDDLEWARE_HANDSHAKE, function (req, next) {
req.session.get('isUserAuthorized', function (err, value) {
if (value) {
next();
} else {
next('Session ' + req.session.id + ' was not authorized');
}
});
});

scServer.on('connection', function (socket) {
// Emit a 'greet' event on the current socket with value 'hello world'
socket.emit('greet', 'hello world');

    /*
        Store that socket's session for later use.
        We will emit events on it later - Those events will 
        affect all sockets which belong to that session.
    */
    activeSessions[socket.session.id] = socket.session;
    socket.session.set('isUserAuthorized', true, callback);

});

req.session is always undefined.

The handshake called before socket.connected. If req.session is set and true, client is authorize the previous connection method, not require authorization again.

req.session is unavailable always and I don't understand why.

from socketcluster.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.