Skip to main content

Listeners and Error Handling

The Client has two types of event listeners: global and query.

Global Listener

The listener can be attached to the client when use connect method to Event Delivery Service

Example

let globalListener = function(event) {
if (event.type === "open") {
console.log('EDS connection established: ', event);
} else if (event.type === "properties") {
console.log("EDS assigned properties: ", event.data);
} else if (event.type === "server-global-error") {
console.log(`EDS replied with an Error: `, event);
}else if (event.type === "server-query-error") {
console.log(`EDS replied with an Error for query '${event.query}': `, event);
} else if (event.type === "client-global-error") {
console.log(`Client global error: `, event);
} else if (event.type === "client-query-error") {
console.log(`Client error for query '${event.query}': `, event);
} else if (event.type === "message") {
console.log('Message event: ', event);
} else if (event.type === "close") {
console.log('EDS connection closed: ', event);
}
};

let connection = PhotoniqEdsSdk.connect({
host: "<YOUR-PHOTONIQ>.photoniq.macrometa.io",
customerId: "<YOUR-CUSTOMER-ID>",
apiKey: "<YOUR-API-KEY>",
fabric: "<YOUR-FABRIC>",
}, globalListener);

EDSEvent Schema

The globalListener function returns EDSEvent instance which has the next schema:

PropertyTypeRequredDescription
typeEDSEventTypeYesList of event types generated by EDS driver
connectionConnectionYesAn instance used for the WebSocket/SSE connection
dataanyNoPresent for events with data like message, cnnection-id types
codenumberNoError code, only present for server side errors
messagestringNoError message, only present in error's event
querystringNoError for a query, only present for query's errors

EDSEventType Schema

The type property can have the next EDSEventType enum values:

TypeDescription
openOpenned WebSocket/SSE connection
propertiesReceived properties of connection
server-query-errorServer-side error related to a query
server-global-errorServer-side error not related to a query
client-query-errorClient-side error related to a query
client-global-errorClient-side error not related to a query
messageMessage event
closeClosed WebSocket/SSE connection

Query Listeners

The functions listen to events related to a specific query and are divided into result and error listeners.

Result Listener

It listens only for events with event.type === "message" and is passed as the second parameter to the retrieve, retrieveAndSubscribe, and subscribe methods:

let sql = "SELECT * FROM <YOUR-COLLECTION> WHERE key=<YOUR-KEY>";
let resultListener = (event) => {
console.log(`Message event: `, event);
};
querySet.retrieve(sql, resultListener);

Error Listener

The error listener handles error messages related to a query, which can have the types event.type === "server-query-error" or event.type === "client-query-error"

let sql = "SELECT * FROM <YOUR-COLLECTION> WHERE key=<YOUR-KEY>";
let resultListener = (event) => {
console.log(`Message event: `, event);
};
let errorListener = (event) => {
console.log(`Error event: `, event);
};
querySet.retrieve(sql, resultListener, errorListener);