@inrupt/solid-client-notifications / websocketNotification / WebsocketNotification

Class: WebsocketNotification#

websocketNotification.WebsocketNotification

Constructor for a WebSocket Notification instance, which allows subscribing to resources in the solid ecosystem. See the Solid Notifications Protocol Specification for more details.

import { getDefaultSession } from '@inrupt/solid-authn-browser';
// or for node.js:
//   import { Session } from '@inrupt/solid-authn-node';

const session = getDefaultSession();
// for node.js:
//   const session = new Session();
//   await session.login({
//     oidcIssuer,
//     clientId,
//     clientSecret,
//   });

const socket = new WebsocketNotification(parentContainerUrl, {
  fetch: session.fetch,
});

socket.on("message", (notification) => {
  console.log("Change:", notification);
});

// Connect for receiving notifications:
await socket.connect();

// later:
socket.disconnect();

Hierarchy#

  • LiveNotification

    WebsocketNotification

Table of contents#

Constructors#

Methods#

Constructors#

constructor#

new WebsocketNotification(topic, options?)

Parameters#

Name

Type

topic

string

options?

NotificationOptions

Defined in#

src/websocketNotification.ts:96

Methods#

connect#

connect(providedEndpoint?, providedSubprotocol?): Promise<void>

Connects the websocket to start receiving notifications. If no providedEndpoint or providedSubprotocol parameter is present, then those will automatically be discovered based on the capabilities of the host of the resource that you’re subscribing to notifications for.

Parameters#

Name

Type

providedEndpoint?

string

providedSubprotocol?

string

Returns#

Promise<void>

Defined in#

src/websocketNotification.ts:107


disconnect#

disconnect(): void

Returns#

void

Defined in#

src/websocketNotification.ts:169


on#

on(event, listener): WebsocketNotification

Emitted when the connection is established

Parameters#

Name

Type

event

"connected"

listener

() => void

Returns#

WebsocketNotification

Defined in#

src/websocketNotification.ts:35

on(event, listener): WebsocketNotification

Emitted when the connection is closed

Parameters#

Name

Type

event

"closed"

listener

() => void

Returns#

WebsocketNotification

Defined in#

src/websocketNotification.ts:40

on(event, listener): WebsocketNotification

Emitted when a valid notification is received, the payload is a activitystreams Activity.

Parameters#

Name

Type

event

"message"

listener

(notification: object) => void

Returns#

WebsocketNotification

Defined in#

src/websocketNotification.ts:48

on(event, listener): WebsocketNotification

Emitted when an error is encountered on the WebSocket

Parameters#

Name

Type

event

"error"

listener

(error: ErrorEvent) => void

Returns#

WebsocketNotification

Defined in#

src/websocketNotification.ts:53


setSessionFetch#

setSessionFetch(sessionFetch?): void

Allows setting a WHATWG Fetch API compatible function for making HTTP requests. When @inrupt/solid-client-authn-browser is available and this property is not set, fetch will be imported from there. Otherwise, the HTTP requests will be unauthenticated.

Parameters#

Name

Type

Default value

sessionFetch

(input: RequestInfo, init?: RequestInit) => Promise<Response>

crossFetch

Returns#

void

Defined in#

src/notification.ts:131