Feathers: Custom events not triggered in client app after create success

Created on 16 Apr 2018  路  14Comments  路  Source: feathersjs/feathers

Hi,

I have an use case, 2 client applications(Client A, Client B) listening to 1 feathers server.

Client A is connected via Feathers Socket.
Client B is used via REST (Non feathers application)

when i create or post data from Client B via REST i can insert successfully through and after struggling to send or notify Client A with some message via Custom Event.

Appreciate some one shares pseudo code for custom event trigger in both Angular UI app and node server side.

Most helpful comment

I just double checked and everything seems to be working fine as documented. I updated the feathers-chat to include the event that I want to send in src/services/messages.service.js:

// Initializes the `messages` service on path `/messages`
const createService = require('feathers-nedb');
const createModel = require('../../models/messages.model');
const hooks = require('./messages.hooks');

module.exports = function (app) {
  const Model = createModel(app);
  const paginate = app.get('paginate');

  const options = {
    name: 'messages',
    Model,
    paginate,
    events: ['myEvent']
  };

  // Initialize our service with any options it requires
  app.use('/messages', createService(options));

  // Get our initialized service so that we can register hooks and filters
  const service = app.service('messages');

  service.hooks(hooks);
};

Then I created an after create hook for the messages service where it sends myEvent (src/hook/send-event.js):

// Use this hook to manipulate incoming or outgoing data.
// For more information on hooks see: http://docs.feathersjs.com/api/hooks.html

// eslint-disable-next-line no-unused-vars
module.exports = function (options = {}) {
  return async context => {
    context.service.emit('myEvent', { hello: 'world' });

    return context;
  };
};

On the client I listened to it just like any other event (adding it here):

client.service('messages').on('myEvent', data => {
  console.log('Got myevent', data);
});

And it showed up whenever someone created a new message. How to listen to socket events when not using Feathers on the client is documented in the Socket.io client docs.

All 14 comments

Custom events are documented here and how to add them to an existing adapter is shown here.

The custom event can be triggered by calling service.emit('customEvent', data) on the server and listened to with app.service('myservice').on('customEvent') when using Feathers on the client.

Thanks @daffl !

I studied the document thoroughly for past few days and custom event through channel is never working. But i succeed working with default service events like ['create', 'patch', 'find']

Also, none of the pages in your document completely covered Feathers client custom event.

Like you already mentioned i already tried how to emit and listen but its not triggering or listening in client UI.

Can you please create a boilerplate how to emit and listen custom events similar like feathers-chat boilerplate?

I just double checked and everything seems to be working fine as documented. I updated the feathers-chat to include the event that I want to send in src/services/messages.service.js:

// Initializes the `messages` service on path `/messages`
const createService = require('feathers-nedb');
const createModel = require('../../models/messages.model');
const hooks = require('./messages.hooks');

module.exports = function (app) {
  const Model = createModel(app);
  const paginate = app.get('paginate');

  const options = {
    name: 'messages',
    Model,
    paginate,
    events: ['myEvent']
  };

  // Initialize our service with any options it requires
  app.use('/messages', createService(options));

  // Get our initialized service so that we can register hooks and filters
  const service = app.service('messages');

  service.hooks(hooks);
};

Then I created an after create hook for the messages service where it sends myEvent (src/hook/send-event.js):

// Use this hook to manipulate incoming or outgoing data.
// For more information on hooks see: http://docs.feathersjs.com/api/hooks.html

// eslint-disable-next-line no-unused-vars
module.exports = function (options = {}) {
  return async context => {
    context.service.emit('myEvent', { hello: 'world' });

    return context;
  };
};

On the client I listened to it just like any other event (adding it here):

client.service('messages').on('myEvent', data => {
  console.log('Got myevent', data);
});

And it showed up whenever someone created a new message. How to listen to socket events when not using Feathers on the client is documented in the Socket.io client docs.

Thanks @daffl ! I tried your sample and now its worked.

I missed custom event emit after hook callback. I'm sure above pseudo-code will help many.

Great job @daffl and keep going your good work !

Hi @daffl, i'm trying to reproduce your example. I've passed the custom event in service option, and emit the event in after create hook. But still, the client couldn't catch the event. However i can catch all the default events. What possibly goes wrong?

edit 1: i can only catch the default events when no custom event is registered in service option
edit 2: Now i can catch the custom event and the default event, but the client receives null custom event data.

Is is necessary for for emitting custom event we need to use hooks ? and is that custom events are emitted at service level not at app level ?

App level custom events are not possible at the moment. You can app.service('myservice').emit('customevent') anywhere, not just in hooks.

Hi @daffl , thank you for your insight.

Service level custom events work great, but how to do a simple socket.emit and socket.on?
Hypothetically can I join a custom channel/room besides '_anonymous_' and '_authenticated_' without going to the whole "set channels/rooms in user object"? Or is this considered the proper way?

I read this several times, but client and server just won't communicate:
https://docs.feathersjs.com/api/application.html#publishevent--publisher
https://docs.feathersjs.com/api/channels.html#apppublishevent-fn

I tried app.emit('custom'), socket.emit('custom') from the client but the server just won't listen. 馃槃
app.emit would just return false.
I have app.publish('custom') and app.on('custom') in channels.js.
I also fiddled with app.eventMappings with no success.

If I check app in my server it does show my custom app event registered, so issue must be client-side?

Update: actually I just found out app.emit would emit a client-side event only and it's only for that very client ...

Feathers currently does not support top level, only service specific events and only custom events sent from the server to the client not the other way around (that's what service method calls are for).

Thanks, are there any plans to support app level in the future?

Maybe if there are good use cases that can't be already handled at the service level. You can already join a custom channel/room by using params.connection e.g. in a custom service or hook.

Hi @daffl ,

I have multi channels created and joined channelA, channelB, channelC, channelD, channelE. Now in service after hook, How can i emit custom event only to particular channel i.e., channelB, channelC.

Because app.service('myservice').emit('customevent') listens all 5 channels instead of particular 2 channels i expect.

Even, channelB.emit('customevent') and channelC.emit('customevent') something will be helpful!

https://docs.feathersjs.com/api/channels.html#apppublishevent-fn

https://docs.feathersjs.com/api/services.html#publishevent--publisher

Use an array with all the channels you like the message broadcasted to.

I keep telling myself "read the docs and think outside the box". :)

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue with a link to this issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

perminder-klair picture perminder-klair  路  3Comments

harrytang picture harrytang  路  3Comments

huytran0605 picture huytran0605  路  3Comments

arve0 picture arve0  路  4Comments

rrubio picture rrubio  路  4Comments