Collection Events

Chapter 5: How to send events to describe changes in a collection

Just like with the model change events, services are required to send events whenever a value has been added or removed from a collection.

Add event

An add event1 is, as the name suggests, an event sent whenever a value is added to a collection. The subject has the following pattern:

event.<resource>.add

Where resource is the resource ID of the collection.

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

  • value - value being added
  • idx - zero based index of where the value was added

In code it could look like this:

	nats.publish("event.example.bar.add", JSON.stringify({
		"value": "bar",
		"idx": 2
	}));

Remove event

The remove event2 is similar to the add event but the subject is instead suffixed by .remove:

The payload is a JSON object with the following property:

  • idx - zero-based index of the value being removed

In code it looks like this:

	nats.publish("event.example.bar.remove", JSON.stringify({
		"idx": 0
	}));