# Notifications

ESS can publish WebSocket notifications for create/update/delete operations on a [Resource](https://docs.inrupt.com/reference/glossary#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](https://inrupt.github.io/solid-client-notifications-js/classes/websocketNotification.WebsocketNotification.html#constructor) constructor to create a **`WebsocketNotification`** object. The constructor accepts the following:
  * Resource URL, and
  * An options object with an authenticated [fetch()](https://inrupt.github.io/solid-client-authn-js/browser/functions.html#fetch) function.

    {% hint style="warning" %} To subscribe to a Resource, you must authenticate as a user with **`Read`** to the Resource. For details on user authentication, see [Authentication](https://docs.inrupt.com/sdk/javascript-sdk/authentication). {% endhint %}
* Use the [on](https://inrupt.github.io/solid-client-notifications-js/classes/websocketNotification.WebsocketNotification.html#on) function to listen for events. The function accepts the following:
  * The event type (e.g., **`"message"`**), and
  * A callback function to process the event.
* Use the [connect](https://inrupt.github.io/solid-client-notifications-js/classes/websocketNotification.WebsocketNotification.html#connect) function to connect to the WebSocket server.
* Use the [disconnect](https://inrupt.github.io/solid-client-notifications-js/classes/websocketNotification.WebsocketNotification.html#disconnect) function to disconnect from the WebSocket server.

When subscribing to a Container, the application receives notifications when the Container is created, deleted, or modified (such as when files are added or removed from the Container)

When subscribing to [SolidDataset](https://docs.inrupt.com/reference/glossary#soliddataset) files or regular files (e.g., **`.jpg`**, **`.json`**), the application receives notifications when the target files are modified or deleted.

#### Example[#](#example)

The following example subscribes to change notifications for a Container **`https://storage.inrupt.com/<some identifier>/some-container/`** and logs the event messages to your console.

{% hint style="info" %}
**Note**

* When subscribing to a Container, the application receives notifications when the Container is created, deleted, or modified (such as when files are added or removed from the Container).
* For brevity, the authentication logic has been omitted. For details on user authentication, see Authentication.
  {% endhint %}

```javascript
import { getDefaultSession, fetch } from "@inrupt/solid-client-authn-browser";
import {
  WebsocketNotification,
} from "@inrupt/solid-client-notifications";

const containerUrl = "https://storage.inrupt.com/<some identifier>/some-container/";

// ... authentication logic has been omitted

const websocket = new WebsocketNotification(
  containerUrl,
  { fetch: fetch }
);

websocket.on("message", (message) => { console.log(JSON.stringify(message)) });

websocket.connect();
```

#### Sample Event

The following is a sample event emitted by the [Inrupt’s PodSpaces](https://docs.inrupt.com/podspaces/podspaces).

```javascript
{
   "@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"
   ],
   "object":{
      "id":"https://storage.inrupt.com/<some identifier>/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"
}
```

<table data-header-hidden><thead><tr><th width="128.06964111328125">Field</th><th>Description</th></tr></thead><tbody><tr><td><strong><code>@context</code></strong></td><td>An array containing the JSON-LD contexts for the notification event itself.</td></tr><tr><td><strong><code>id</code></strong></td><td>String that contains an identifier for the event.</td></tr><tr><td><strong><code>type</code></strong></td><td><p>An array identifying the event type:</p><pre class="language-javascript"><code class="lang-javascript">[
   "http://www.w3.org/ns/prov#Activity",
   "&#x3C;Action>"
]
</code></pre><p>Where <strong><code>"&#x3C;Action>"</code></strong> can be one of the following values:</p><ul><li><strong><code>"Create"</code></strong></li><li><strong><code>"Delete"</code></strong></li><li><strong><code>"Update"</code></strong></li></ul></td></tr><tr><td><strong><code>object</code></strong></td><td>The resource object:</td></tr><tr><td></td><td></td></tr><tr><td><strong><code>object.id</code></strong></td><td>String indicating the Resource URL.</td></tr><tr><td><strong><code>object.type</code></strong></td><td><p>An array indicating the Resource types.</p><div data-gb-custom-block data-tag="hint" data-style="info" class="hint hint-info"><p><strong>Tip</strong></p><p>Because a Container object is also an RDF Resource, to determine if the object is a non-Container RDF Resource, check that neither <strong><code>http://www.w3.org/ns/ldp#Container</code></strong> nor <strong><code>http://www.w3.org/ns/ldp#BasicContainer</code></strong> appear as elements of <strong><code>object.type</code></strong>.</p></div></td></tr><tr><td><strong><code>object.id</code></strong></td><td>String indicating the Resource URL.</td></tr><tr><td><strong><code>object.type</code></strong></td><td><p>An array indicating the Resource type(s).</p><p>Tip</p><p>Because a Container object is also an RDF Resource, to determine if the object is a non-Container RDF Resource, check that neither <strong><code>http://www.w3.org/ns/ldp#Container</code></strong> nor <strong><code>http://www.w3.org/ns/ldp#BasicContainer</code></strong> appear as elements of <strong><code>object.type</code></strong>.</p></td></tr><tr><td><strong><code>published</code></strong></td><td>The date and time the event is published.</td></tr></tbody></table>
