Custom service method completes but clients never receive a real-time event
0 reputation · 03 Jul 2022, 20:45 UTC
0 reputation · 03 Jul 2022, 20:45 UTC
I have a Feathers v5 service with a custom method (e.g. approveOrder) registered alongside the standard CRUD methods. When clients call it over Socket.io, the method runs and the database change succeeds, but connected clients never receive any created, updated, or patched event. Standard patch calls on the same service broadcast as expected.
My understanding from the real-time documentation is that only the standard service methods emit events automatically, and custom methods require an explicit context.service.emit() call or an after hook. What is unclear is the intended convention: which event name should a custom method emit so that feathers-socketio publishes it to the right channels, and whether that emission should live inside the method body or in an after hook.
I also run multiple server instances, so any event emitted on one node needs to reach clients connected to other nodes.
emit() with a custom event name the officially supported pattern for custom methods in v5, or is wrapping the logic in a standard patch preferred?30775 reputation · 04 Jul 2022, 05:45 UTC
1. Is calling context.service.emit() the officially supported pattern for custom methods in v5?
Yes. In v5 only the standard CRUD methods emit events automatically. A custom method must explicitly call context.service.emit(eventName, result) (or do it in an after‑hook that has access to context.result). A wrapper patch is unnecessary unless you want to keep the same CRUD semantics.
2. Where should the emit happen – inside the method or an after‑hook?
Inside the method body, right after the business logic succeeds. The hook context does not have the method’s return value unless you manually pass it, and emitting in a hook would delay the event until after the hook chain, which can hide errors from the client.
3. Do custom events propagate across a multi‑server setup the same way?
Yes, provided the event name is registered in the service’s events array (or via service.publish()) and you emit it with context.service.emit(). Feathers‑socketio will then publish the event to all connected nodes through the configured pub/sub (Redis, etc.). No extra configuration is required beyond what you already use for standard events.
events list.app.use('/orders', ordersService);
const orders = app.service('orders');
orders.events = ['orderApproved']; // or orders.publish(...)
async function approveOrder(context) {
const result = await businessLogic(context);
// Emit after successful completion
context.service.emit('orderApproved', result);
return result;
}
Did you add the custom event name (e.g., orderApproved) to the service’s events array? If it’s missing, the event will never reach clients, regardless of the emit call.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.