简体   繁体   中英

How to add parameters to a FeathersJS socket connection

I'm developing an app that uses a FeathersJS server, and an Electron app that uses FeathersJS Socket.io client to connect to the server. I'd like to use channels in the Electron app to get notified when some data in the server has changed. The Electron app is an "autonomuos" app, so there isn't any user authentication or interaction. The Electron app will have a unique id that represents each machine it is running on.

Taking into account that there isn't any kind of authentication, how can I add the Electron app's unique id in the socket.io connection so that this id is received in the server and it can create the corresponding channel in the FeathersJS server?

Thank you!

How to pass information on the server from the Socket.io transport to Feathers through a Socket.io middleware is documented in the Socket.io transport API . There also is an example how to pass query parameters from the client to the server in the Socket.io documentation . Put together it looks like this on the client:

const socket = io('http://feathers-server.com', {
  query: {
    token: 'cde'
  }
});

And like this on the server:

app.configure(socketio(function(io) {
  io.use(function (socket, next) {
    socket.feathers.token = socket.handshake.query.token;
    next();
  });
}));

socket.feathers is the exact same as the channel connection object so it will now have a token property you can use to join as a channel:

app.on('connection', connection => {
  if (connection.token) {
    app.channel(connection.token).join(connection);
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM