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:
Property | Type | Requred | Description |
---|---|---|---|
type | EDSEventType | Yes | List of event types generated by EDS driver |
connection | Connection | Yes | An instance used for the WebSocket/SSE connection |
data | any | No | Present for events with data like message , cnnection-id types |
code | number | No | Error code, only present for server side errors |
message | string | No | Error message, only present in error's event |
query | string | No | Error for a query, only present for query's errors |
EDSEventType Schema
The type
property can have the next EDSEventType
enum values:
Type | Description |
---|---|
open | Openned WebSocket/SSE connection |
properties | Received properties of connection |
server-query-error | Server-side error related to a query |
server-global-error | Server-side error not related to a query |
client-query-error | Client-side error related to a query |
client-global-error | Client-side error not related to a query |
message | Message event |
close | Closed 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);