ERPNext.com Frappe Cloud Support Partners Foundation Frappe School

Subscribe in backend to a frappe.publish_realtime event within same site

Hi together,

using frappe.publish_realtime an “event” is triggered (via socket.io), to which you can subscribe in a frontend event as described within https://frappeframework.com/docs/v13/user/en/basics#realtime.

In a similar field, when we have to transmit data to another site, we can implement “event streaming” - then it would be backend-to-backend communication. https://docs.erpnext.com/docs/v13/user/manual/en/automation/event_streaming.

The reason why I am asking: We have very often the situation, that within one document, we show a few information from another document and we would like to make sure, that both “are in sync”.
Example: Within “Customer” we have a custom link and data-field (with fetch-from based on the link-field) implemented, to show eg. a information from the primary Contact of a Customer.
If we would change that information within the Contact, the related field within the Customer would never be updated, as a “fetch-from” will do that only “once” (when selecting a document within the link-field).

My “hope” was, that there is a more elegant approach than implement the update to all “dependent” fields within the onSave-Event of Customer, but instead, just publish an event eg. “Contact field xx updated” and all subscribers can do whatever is necessary… But that would require to subscribe to a event within the backend-part, as we can not rely on a user to have that document open in their browser and therefore the client-event could be implemented…

Is it possible to subscribe to a realtime-Event within the backend?
I guess the answe is “no”, and the best solution would be to directly call a backend method or enqueue a background job during the onSave-event…?

Any ideas to that?

Thanks!

I think it’s good idea if ERPNext update any field with “fetch from” if the source field data change at any time, however i think using on_update hook is exactly a subscription to server event because the method execute instantly

@Patrick.St, via frappe.publish_realtime in backed, is not available!

For that purpose is why we have doc_events on the hooks.py and you can subscribe to any event raised by the backend, on the documents, including on_change (aka on field change)

1 Like

I would have liked an event-based subscription to such changes/updates even better, but this confirms my understanding.

Thank you!