How to add more parameters on feathersjs service method with primus

1.5k Views Asked by At

I checked this link https://docs.feathersjs.com/real-time/primus.html to setup additional parameters on service method but I didn't figure out how to do it.

Below is my service class:

create(id, shellId, params) {
     ...

  }

below is the primus configuration:

 app.configure(primus({
    transformer: 'websockets',
    timeout: false
  }, (primus) => {
    primus.library();
    primus.save(path.join(__dirname, '../public/dist/primus.js'));
  }))

In the manual, it mentions something like below:

primus.use('todos::create', function(socket, done){
    // Exposing a request property to services and hooks
    socket.request.feathers.referrer = socket.request.referrer;
    done();
  });

but I am not sure what this function is doing. And also I tried to add above code in my application, I will get socket.request is undefined error. How to add more parameters on service class in this case?

1

There are 1 best solutions below

0
On BEST ANSWER

You can only use the official service methods and their signatures, in the case of create it is data for the entire data object and params for additional parameters. Your service class create looks something like this:

class MyService {
  create(data, params) {
    const { shellId, id } = data;
    // Do something here

    return Promise.resolve({ /* some data here */ });
  }  
}

app.use('/myservice', new MyService());

On the client you can use it through the Feathers client or a direct Primus connection like this:

primus.send('myservice::create', {
  id: 'test',
  shellId: 'testing'
}, (error, data) => {
  console.log('Returned data', data);
});