Search code examples
socket.iofeathersjs

FeathersJS socketio client disconnect?


I'm developing an Electron app that connects to a FeathersJS server using FeathersJS SocketIO client. It works properly using the code provided at FeathersJS documentation, but I'd like the possibility to connect/disconnect the client from the server, basically to connect to another server (with exactly the same services). The example would be: I have 2 FeathersJS servers with exactly the same services but with different data stored, and I want my Electron app to be able to connect to server number 1, then disconnect and connect to server number 2.

Is there any way to do this?


Solution

  • The most straightforward way is probably to initialized two Feathers applications that you can swap out accordingly in your Electron app. A slightly more hacky way would be to replace the connection on the application (app.io) and the initialized client services (service.connection):

    const replaceConnection = (app, socket) => {
      app.io = socket;
    
      Object.keys(app.services).forEach(name => {
        app.service(name).connection = socket;
      });
    
      return app;
    }