Services
Change-notify (notify/v2)
The standard WebSocket interface through which a Factory+ service sends a client the new content of each resource it watches, with the client's own permissions applied.
Change-notify is a WebSocket interface that sends a client the new content of a service resource whenever it changes.
Clients of a Factory+ service often need to act as soon as something the service holds changes. That might be a configuration entry, an access control list or the list of objects in a class. Polling the HTTP API is slow to notice changes and puts load on the service. Factory+ therefore defines a standard change-notify interface that any HTTP service can offer. A client opens one WebSocket to the service and asks to watch the HTTP resources it cares about. The service sends the new response for a resource each time it changes.
Each update carries the content of the changed resource. The client does not need a second request to find out what changed, and there is no gap in which the resource could change again before the client reads it.
Each update is built with the client’s own permissions, exactly as a plain HTTP request from that client would be. The set of changes a principal may see is specific to that principal, so most notifications have a single consumer. For that reason the interface runs over a WebSocket provided by the service itself rather than over MQTT, because MQTT would need a separate topic and access rule for every client.
This is version 2 of the interface. The service specification says when a service should offer it. Each service page lists the paths that can be watched through it.
Versions and paths
The interface lives at the top-level path notify/v2. The path is resolved against the service URL like any other API path. A service at https://example.com/configdb/ offers it at wss://example.com/configdb/notify/v2.
The whole notify/ prefix is reserved for change-notify interfaces. notify/v2 is reserved for this interface and compatible extensions to it.
| Version | Interface | Status |
|---|---|---|
| 0 | Last_Changed metrics in the service’s Sparkplug birth (see below) | Deprecated |
| 1 | notify/v1, an experimental interface over per-client Sparkplug Devices | Experimental. It is not part of the framework and is not described here. |
| 2 | notify/v2, the WebSocket interface on this page | Current |
Opening a connection
The client opens a WebSocket to the notify/v2 path under the service URL. The scheme is ws: for an http: service URL and wss: for an https: one. The client SHOULD NOT request a WebSocket sub-protocol.
Authentication
The client authenticates inside the WebSocket instead of with an Authorization header, because browsers cannot set HTTP headers on a WebSocket request.
- The first message the client sends MUST be a text message holding the string
Bearer, exactly one space, and a bearer token for the service being contacted. Nothing may follow the token, not even a trailing space or newline.Beareris case-sensitive. - The client MUST NOT send anything else until the service replies.
- The service MUST reply with a text message exactly three characters long, holding one of the status codes below.
The token is one the service accepts as an HTTP bearer token. It is normally a token from the service’s own POST /token endpoint, or an OIDC token where the service accepts those. See Authentication for both.
| Code | Meaning |
|---|---|
| 200 | The client is authenticated and may send subscription requests. |
| 400 | The service did not understand the first message. |
| 401 | The token is not valid. |
| 403 | The client may not use the notify interface at all. |
| 404 | This interface is not available on this service. |
| 503 | This interface is temporarily unavailable. |
After any code other than 200, the service closes the WebSocket. Some of these failures may instead be reported during the WebSocket handshake, before the first message. A client MUST handle a refused WebSocket upgrade in the same way.
Messages
After authentication, every message in either direction MUST be a text frame holding a single JSON value. Message boundaries are significant. Messages from the client are subscription requests. Messages from the service are subscription updates.
Subscriptions
The service keeps a list of subscriptions for each open WebSocket. Each subscription corresponds to a request to the service’s ordinary HTTP API. The service sends a new response whenever the result of that request changes.
A client could get the same result by polling the HTTP request and ignoring identical responses. Only requests that are safe to repeat (idempotent requests) may therefore be subscribed to.
Each subscription is identified by a UUID that the client chooses when it makes the request.
- The client MUST make each subscription UUID unique for the life of the connection.
- The client MUST NOT reuse the UUID of a closed subscription on the same connection.
- The client is responsible for remembering its own subscriptions. The service does not report them back.
- When the WebSocket closes, for any reason, the service drops all its subscriptions. The client then follows the steps under Reconnecting.
Requests
A subscription request is a JSON object with these keys, plus the keys the method needs.
| Key | Value |
|---|---|
uuid | The UUID of the subscription. |
method | WATCH, SEARCH or CLOSE. Method names are case-sensitive. |
WATCH and SEARCH open a new subscription. CLOSE ends one. A service supports subscriptions only to the requests it lists on its service page. Any other request gets subscription status 404.
Updates
An update from the service is a JSON object with these keys, plus any the subscription type adds.
| Key | Value |
|---|---|
uuid | The UUID of the subscription this update belongs to. |
status | The status of the subscription (see below). |
response | An HTTP response object. Present on every 201 and 200 update, and optional otherwise. |
The service’s view of a subscription’s state is the authoritative one. The service MAY close a subscription at any time.
Subscription status
The status key gives the state of the subscription. It is separate from the status inside response, which is the status of the watched resource. A 2xx status means the subscription is open. A 4xx or 5xx status means it is closed and no further updates will come for it.
| Status | Meaning |
|---|---|
| 201 | The first response for a new subscription. It gives the complete current state. |
| 200 | A later response, sent because the watched resource changed. |
| 400 | The request was not understood, and no subscription was created. A WATCH with no request, or a SEARCH whose parent does not end in /, gets this status. |
| 403 | The client may not make this subscription. |
| 404 | The service does not support subscribing to this request. |
| 410 | The subscription was closed because the client asked. |
| 500 | The subscription was closed because of an error in the service. |
| 503 | The service closed the subscription for its own reasons. |
A client MUST interpret any other status by its first digit. The meanings of 1xx and 3xx are not defined. If a client receives a 1xx or 3xx status it does not understand, it MUST close the WebSocket.
HTTP response objects
The response key holds the HTTP response the client would have received for the watched request. It is a JSON object with these keys.
| Key | Value |
|---|---|
status | The HTTP status code. |
headers | Optional. The HTTP headers that matter to the meaning of the response, as an object or an array, read as in the Fetch standard. Headers such as Cache-Control are normally left out, and Content-Type defaults to application/json. |
body | Optional. The JSON value that would have been the response body. |
Only resources with JSON responses can be watched, because only JSON bodies can be carried. The HTTP status means the same as in a plain response, with three additions.
- 201 MAY be used to say a resource has been created, even though a
GETwould not normally return 201. - 204 MAY be used to say a resource exists but its content is not being sent.
- 412 MAY be used, where a subscription covers several resources, to say that one resource has left the set being watched.
An HTTP error does not close the subscription. A subscription whose first update is 201 with an HTTP 403 stays open. If the client is later granted access, it receives a 200 update with an HTTP 200. In the same way, a 201 update with an HTTP 404 means the resource does not exist yet. A later 200 update with an HTTP 201 means it has been created.
Where a client does not have access, a service MAY respond with 403 or hide the resource with 404. The service MUST follow the same choice consistently. The choice MUST match what the plain HTTP API returns for the same request.
WATCH
A WATCH subscription follows the response to a single HTTP request. It behaves as if the client polled the request and waited for the response to change. The request carries a request key, which is an object with these keys.
| Key | Value |
|---|---|
url | The URL to watch, relative to the service URL. It MAY include a query string, and is URL-decoded as a relative URL. |
method | Optional. The HTTP method, in upper case. The default is GET. HEAD gives the same updates with the bodies left out. |
headers | Optional. Request headers, as an object or an array. |
body | Optional. A JSON request body. Content-Type defaults to application/json. |
method, headers and body are not normally needed, because a watched request has to be idempotent. A service MAY support watching a POST request that is idempotent, such as an endpoint that runs a complex search.
This example watches GET v1/example/abc-123. It is shown in YAML for readability.
# Client request
uuid: 6a1f0d3e-8c2b-4f7a-9e5d-1b3c4d5e6f70
method: WATCH
request:
url: v1/example/abc-123
---
# First update
uuid: 6a1f0d3e-8c2b-4f7a-9e5d-1b3c4d5e6f70
status: 201
response:
status: 200
body:
name: abc-123
---
# The resource is deleted
uuid: 6a1f0d3e-8c2b-4f7a-9e5d-1b3c4d5e6f70
status: 200
response:
status: 404
SEARCH
A SEARCH subscription follows a set of resources. The set is all the immediate children of a parent URL, optionally narrowed by a filter. Its updates are the same as a set of plain GET requests, one for each child, plus following the parent to see which children exist.
Suppose a service has GET v1/example/, which returns an array of child names, and GET v1/example/{name} for each child. A client could keep up with all the children by polling these URLs. A SEARCH with the parent v1/example/ does the same job in one subscription.
The request carries these keys.
| Key | Value |
|---|---|
parent | The URL of the parent, relative to the service URL. It MUST end with /. |
filter | Optional. A JSON value that selects which children to follow (see Filtering). |
Each child is identified by its child path. A child path is a string with no / in it and gives the child’s URL when resolved against the parent URL. The parent URL ends in / so that this resolution works.
Update forms
A SEARCH subscription sends three forms of update.
- A full update has a
childrenkey, which maps every current child path to an HTTP response object for that child. Itsresponseis the parent’s response, normally{"status": 204}. The first update of a subscription (status 201) MUST be a full update. - A child update has a
childkey holding one child path. Itsresponseholds that child’s new HTTP response. A child update with an HTTP 404 means the child has been removed. - A no-access update has neither key. Its
responseis the parent’s response, normally an HTTP 403 or 404. It means the client cannot see the set of children at all. On receiving one, the client MUST discard every child response it holds for the subscription. The service MUST send a full update before it sends any further child update.
The parent’s own body is never sent, because it normally lists the child paths and the update already shows those. A successful response for the parent MUST use status 204 rather than 200 or 201, to show that the body has been left out.
Where a client can learn that a child exists but would get a 403 when reading it, the service SHOULD include that child in children with an HTTP 403. This keeps the results the same as the plain HTTP API.
Example
A subscription request:
uuid: eb546f59-26c1-4c80-b40b-992401396bfb
method: SEARCH
parent: v1/example/
The first update, a full update showing two children:
uuid: eb546f59-26c1-4c80-b40b-992401396bfb
status: 201
response:
status: 204
children:
abc-123:
status: 200
body:
name: abc-123
xyz-789:
status: 200
body:
name: xyz-789
Later child updates:
# An existing child changes
uuid: eb546f59-26c1-4c80-b40b-992401396bfb
status: 200
child: abc-123
response:
status: 200
body:
name: ABC-123
---
# A new child is created
uuid: eb546f59-26c1-4c80-b40b-992401396bfb
status: 200
child: def-234
response:
status: 201
body:
name: DEF-234
---
# The child is removed
uuid: eb546f59-26c1-4c80-b40b-992401396bfb
status: 200
child: def-234
response:
status: 404
Filtering
A filter limits a SEARCH to some of the children. The service applies the filter to each child’s response body as a JSON Merge Patch (RFC 7396). If the patch leaves the body unchanged, the child is selected. Otherwise it is filtered out. This rule has these consequences.
- In practice the filter and the bodies both need to be JSON objects for the filter to be useful.
- A property in the filter selects children whose body has that property with that value.
- A property given as
nullselects children whose body does not have the property at all. - A property missing from the filter allows any value, or none.
- It is not possible to select on a property whose value is
null. - Arrays are compared as a whole. A filter cannot select on individual items in an array.
The service reports selection changes as follows.
- A child that is filtered out does not appear in any full update and gets no child updates.
- If a change brings an existing child into the selected set, the service sends a child update with an HTTP 200.
- If a change takes an already reported child out of the selected set, the service sends a child update with an HTTP 412.
- A new child that is selected is reported with an HTTP 201.
- A selected child that is removed is reported with an HTTP 404.
CLOSE
A CLOSE request ends the subscription with the given uuid and needs no other keys. The service MAY still send updates for the subscription that were already queued. It MUST then send a final update that closes the subscription, normally with status 410. The client MUST NOT reuse that UUID on the same connection.
Ordering and slow clients
Updates for one subscription arrive in order. The first always has status 201. The update that closes a subscription is always the last one sent for it.
If a client reads more slowly than updates arrive, the service MAY combine updates that are waiting to be sent. Intermediate states may then be skipped.
- For a
WATCH, the client MAY receive only the latest response. - For a
SEARCH, the client MAY receive only the latest child update for each child that changed. - For a
SEARCH, an update for the parent that has nochildkey replaces any child updates waiting behind it.
The state the client ends up with is the same as if it had received every update. Clients MUST therefore treat each update as the current state of the resource, not as an event in a log. A client that needs every individual change MUST get it from another source, such as the Sparkplug data on MQTT.
Reconnecting
A WebSocket can close at any time, for example when the network drops or the service restarts. All its subscriptions close with it. To stay up to date, a client follows these steps.
- The client SHOULD wait before reconnecting, with some random variation in the delay. The variation stops many clients reconnecting to a restarted service at the same moment.
- The client MUST authenticate the new connection from the start. If the old token has expired, this may mean getting a new one from
POST /token. - The client MUST send its subscription requests again on the new connection.
- The client MUST treat the new 201 update of each subscription as the complete current state, replacing anything it held from the old connection, because changes made while it was disconnected are not replayed.
Version 0: Last_Changed metrics
In version 0, a service’s Sparkplug birth contains a Last_Changed folder of metrics. The service publishes a new value to one of them whenever something relevant changes. The value is normally the UUID of the thing that changed. A client watching the metric then has to fetch the resource over HTTP to find out what the change was.
Version 0 has three problems that version 2 removes. The meaning of each metric is defined by the service that publishes it. There is no way to limit who sees a change. A client can read a resource that has already changed again by the time its request arrives.
Version 0 is still used by the Directory, which has no notify/v2 interface.