Subscribe to WebSocket Notifications#
Alpha Release
solid-client-notifications
is available as an Alpha release. Subscribing
to changes is currently only supported by Inrupt’s Enterprise Solid Server.
Solid Server can publish WebSocket notifications for
create/update/delete operations on a Resource. Using the
@inrupt/solid-client-notifications
library, applications can
subscribe to WebSocket notifications for a Resource.
Subscribe to Changes#
To subscribe to WebSocket notifications for a particular Resource:
Use the WebsocketNotification constructor to create a
WebsocketNotification
object. The constructor accepts the following:Resource URL, and
An options object with an authenticated fetch() function.
Important
To subscribe to a Resource, you must authenticate as a user with access to the Resource. For details on user authentication, see Authentication.
Use the on function to listen for events. The function accepts the following:
The event type (e.g.,
"message"
), andA callback function to process the event.
Use the connect function to connect to the WebSocket server.
Use the disconnect function to disconnect from the WebSocket server.
When subscribing to a Container, the application receives notifications when files are added or deleted from the Container as well as when the Container itself is created or deleted.
When subscribing to SolidDataset files or regular files
(e.g., .jpg
, .json
), the application receives notifications
when the target files are modified or deleted.
Example#
The following example subscribes to change notifications for a
Container https://pod.inrupt.com/<user>/some-container/
and
logs the event messages to your console.
Note
When subscribing to a Container, the application receives notifications when files are added or removed from the Container as well as creation/deletion of the Container itself.
For brevity, the authentication logic has been omitted. For details on user authentication, see Authentication.
import { getDefaultSession, fetch } from "@inrupt/solid-client-authn-browser";
import {
WebsocketNotification,
} from "@inrupt/solid-client-notifications";
const containerUrl = "https://pod.inrupt.com/<user>/some-container/";
// ... authentication logic has been omitted
const websocket = new WebsocketNotification(
containerUrl,
{ fetch: fetch }
);
websocket.on("message", console.log);
websocket.connect();
Sample Event#
The following is a sample event emitted by the Pod Server https://pod.inrupt.com.
Note
The event message emitted by your Pod Server may differ.
{
"@context":[
"https://www.w3.org/ns/activitystreams",
{
"state":{
"@id":"http://www.w3.org/2011/http-headers#etag"
}
}
],
"id":"urn:uuid:<uuid>",
"type":[
"http://www.w3.org/ns/prov#Activity",
"Update"
],
"actor":[
"<WebID>"
],
"object":{
"id":"https://pod.inrupt.com/<user>/some-container/",
"type":[
"http://www.w3.org/ns/ldp#BasicContainer",
"http://www.w3.org/ns/ldp#Container",
"http://www.w3.org/ns/ldp#RDFSource",
"http://www.w3.org/ns/ldp#Resource"
]
},
"published":"2021-03-30T01:01:49.550044Z"
}
Field |
Description |
||||
---|---|---|---|---|---|
|
An array containing the JSON-LD contexts for the notification event itself. |
||||
|
String that contains an identifier for the event. |
||||
|
An array identifying the event type: [
"http://www.w3.org/ns/prov#Activity",
"<Action>"
]
Where
|
||||
|
An array containing the WebID of the agent that made the change to the Resource. |
||||
|
The resource object:
|
||||
|
The date and time the event is published. |
Additional Information#
For information on writing data to Solid Pods, see:
Read/Write Structured Data for storing structured data.
Read/Write Files for storing regular files.