Model Events

Chapter 4: How to send events to describe changes in a model

In order for Resgate to keep data synchronized across all clients, services are required to send a model change event1 whenever a model is updated.

Note

All events for a resource must be sent from the same service.

This is needed for Resgate to be sure it applies each event in the right order.

Change event

When sending a change event, the subject of the message will have the following pattern:

event.<resource>.change

Where resource is the resource ID of the model.

The event message is a JSON object with the following property:

  • values - object containing properties that was changed, with their new values.

For a model with the resource ID example.foo, it could look like this:

nats.publish("event.example.foo.change", JSON.stringify({
	values: {
		"name": "Changed name",
		"address": "New street 12"
	}
}));

Tip

A delete action2 is a JSON object used instead of a value when a property has been deleted from a model.
It has the following signature:

{ "action": "delete" }