Search code examples
feathersjs

Feathers unique client id?


I am running a feathers.js application and I am watching for updates on a resource. I want to be able to uniquely identify if I am the client that sent the update. What is the best way to tell that?

If I am logged on as the same user on 2 identical browser windows, I should also be able to tell if I updated the resource from my current browser window.

EDIT: Or is there a way to at least stop sending the update to the client that made it?


Solution

  • This took some digging, but I was able to get this working pretty well with some custom express middleware.

    // app.js
    
    app.configure(
      socketio(io => {
        io.use((socket, next) => {
          socket.feathers.connectionID = socket.client.id;
          next();
        });
      })
    );
    
    // myService.service.js (or wherever you set up your channels)
    
    service.publish((data, context) => {
      return app.channel(...).filter((connection) => {
        const senderConnectionID = context.params.connection.connectionID;
        const thisConnectionID = connection.connectionID;
        return senderConnectionID !== thisConnectionID;
      });
    });
    

    It doesn't require any client-side setup, but in my case I'm using this to identify sessions, so I did make sure I was only creating one socket per page load and sharing it between all my client instances.