Skip to content

Commit

Permalink
Merge pull request #1246 from Macrometacorp/update-eds-client
Browse files Browse the repository at this point in the history
EDS-19: Update EDS SDK
  • Loading branch information
funke-32 authored Oct 3, 2024
2 parents 2524fdc + c2b2b39 commit 4bdb17a
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 36 deletions.
2 changes: 1 addition & 1 deletion docs/photoniq/event-delivery/clients/_category_.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"label": "Client SDKs",
"label": "Client SDK",
"position": 100
}
63 changes: 57 additions & 6 deletions docs/photoniq/event-delivery/clients/connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,86 @@ connection.connect();

### getConfig

Retrieves the configuration of the connection. Returns a [`Config`](/photoniq/event-delivery/clients/#config-schema) instance:
Retrieves the configuration of the connection. Returns a [`Config`](/photoniq/event-delivery/clients/#config) instance:
```js
connection.getConfig();
```

| **Argument** | **Type** | **Description** |
|--------------|------------|------------------------|
| return | [`Config`](/photoniq/event-delivery/clients/#config) | Client configuration of the connection |

### getId

Retrieves the connection ID. Returns a `number` value:
Retrieves the connection ID:
```js
connection.getId();
```

### isConnected
| **Argument** | **Type** | **Description** |
|--------------|------------|------------------------|
| return | `number` | ID of the connection |

### getStatus

Checks status of the connection:
```js
connection.getStatus();
```

The method can return the next `ConnectionType` enum values:

| **Type** | **Description** |
|----------------------|---------------------------|
| `ConnectionType.Closed` | Connection closed |
| `ConnectionType.Connecting` | Connection is opening |
| `ConnectionType.Open` | Connection opened |
| `ConnectionType.Closing` | Connection is closing |


Checks whether the connection is alive. Returns a `boolean` value:
### getProperty

Get property of a connection:
```js
connection.isConnected();
connection.getProperty(name);
```

| **Argument** | **Type** | **Description** |
|--------------|------------|------------------------|
| `name` | `string` | Name of a property |
| return | `string` | Value of a property |


### getProperties

Get all properties of a connection:
```js
connection.getProperties();
```

| **Argument** | **Type** | **Description** |
|--------------|------------|------------------------|
| return | `object` | Object of all property:value pairs |

### querySet

Create a `QuerySet` instance:
Create a [`QuerySet`](/photoniq/event-delivery/clients/query-set-and-batch) instance:
```js
connection.querySet();
```

| **Argument** | **Type** | **Description** |
|--------------|------------|------------------------|
| return | [`QuerySet`](/photoniq/event-delivery/clients/query-set-and-batch) | Query set instance |


### disconnect

Disconnect from the Event Delivery Service:
```js
connection.disconnect();
```

| **Argument** | **Type** | **Description** |
|--------------|------------|------------------------|
| return | `boolean` | `true` if disconnected; `false` if it was not connected |
117 changes: 93 additions & 24 deletions docs/photoniq/event-delivery/clients/index.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
---
sidebar_position: 1
title: Event Delivery Client SDKs
title: Getting Started with Event Delivery Client SDK
---

This section demonstrates some of the tasks you can complete with JavaScript SDKs. Macrometa offers the following SDKs:
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

- [WebSocket](/download/eds-clients/photoniq-eds-ws.min.js)
- SSE (coming soon)
PhotonIQ offers SDK to enable you connect to and deliver event streams to your applications, services, and several other data-volatile use cases. It supports both WebSocket and Server-Sent Events (SSE) connections.

## Pre-requisites
## Quick Start

- Download `photoniq-eds-ws.min.js` or `photoniq-eds-sse.min.js` from section above.
- Attach the module to your html:
This section demonstrates some simple tasks to help get you started using this client SDK in two ways.

This quickstart guide will guide you through:
- Connecting to an event delivery service
- Querying and subscribing that service and receiving query results and updates

<Tabs groupId="sdks">
<TabItem value="commonjs" label="Minified JS">

- Download [`photoniq-eds-sdk.min.js`](/download/eds-clients/photoniq-eds-sdk.min.js) client SDK.

- Attach the Javascript module to html code:
```html
<script src="path/to/photoniq-eds-ws.min.js"></script>
<!-- or -->
<script src="path/to/photoniq-eds-sse.min.js"></script>
<script src="path/to/photoniq-eds-sdk.min.js"></script>
```

## Basic Example
- Basic Example:

Connect to Event Delivery Service, retrieve and subsribe to SQL:
```js
Expand All @@ -29,7 +37,7 @@ let config = {
fabric: "<YOUR-FABRIC>",
};

let connection = PhotoniqEdsWs.connect(config);
let connection = PhotoniqEdsSdk.connect(config);

let querySet = connection.querySet();

Expand All @@ -38,28 +46,89 @@ querySet.retrieveAndSubscribe("SELECT * FROM <YOUR-COLLECTION> WHERE key=<YOUR-K
})
```

</TabItem>

<TabItem value="nodejs" label="Node.JS">

- Install the module:

```
npm i photoniq-eds-sdk
```

- Add dependency in the project:

[Vue](https://vuejs.org/) Framework Example:
```js
import { connect } from 'photoniq-eds-sdk';

export default {
name: 'App',
mounted() {
this.initialize();
},
methods: {
initialize() {
let connection = connect({
host: "<YOUR-HOST>",
customerId: "<YOUR-CUSTOMER-ID>",
apiKey: "<YOUR-API-KEY>",
fabric: "<YOUR-FABRIC>",
});

let qs = connection.querySet();

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

</TabItem>
</Tabs>

:::note
The example uses the WebSocket client. To use an SSE connection, replace `PhotoniqEdsWs` with `PhotoniqEdsSse`.
The example uses a WebSocket connection. To switch to an SSE connection, add connectionTypes: `["sse"]` to the config. For a priority connection that falls back to WebSocket in case of network issues, use connectionTypes: `["sse", "ws"]`.
:::

## Supported Methods

## Config Schema
### Connect

| **Property** | **Type** | **Requred** | **Description** |
|----------------------|-----------|-----------|-----------------------------------|
| `host` | `string` | Yes | Host of the connectionn |
| `fabric` | `string` | No | Fabric to be used. Default is `_system` |
| `customerId` | `string` | Yes | Customer ID credentails |
| `apiKey` | `string` | Yes | ApiKey credentails |
| `pingSeconds` | `number` | No | Seconds between ping-pong messages to the server. Default is `29` |
Create a new [`Connection`](/photoniq/event-delivery/clients/Connection) instance and establish connection to PhotonIQ EDS server:

```js
let connection: Connection = PhotoniqEdsSdk.connect(config);
```

## Disconnect
### Create

To gracefully disconnect from the Event Delivery Service:
Create a new [`Connection`](/photoniq/event-delivery/clients/Connection) instance:

```js
connection.disconnect();
let connection = PhotoniqEdsSdk.create(config);
```

#### `Config` instance schema:

| **Property** | **Type** | **Requred** | **Description** |
|----------------------|-----------|-----------|-----------------------------------|
| `host` | `string` | Yes | Host of the connection |
| `customerId` | `string` | Yes | Customer ID credentails |
| `apiKey` | `string` | Yes | ApiKey credentails |
| `fabric` | `string` | No | Fabric to be used. Default is `_system` |
| `connectionTypes` | `(string `\|` SubConfig)[]` | No | Use type of connection and priority. Default is `["ws"]`. Types: `ws`, `sse` |
| `autoReconnect` | `boolean` | No | Automatically reconnect in case of network issues. Default is `true` |

#### `SubConfig` instance schema:

| **Property** | **Type** | **Requred** | **Description** |
|----------------------|-----------|-----------|-----------------------------------|
| `type` | `string` | Yes | Type of the connection. Value: `ws` or `sse` |
| `customerId` | `string` | No | Customer ID credentails. Default set from `Config` |
| `apiKey` | `string` | No | ApiKey credentails. Default set from `Config` |
| `fabric` | `string` | No | Fabric to be used. Default set from `Config` |
| `pingSeconds` | `number` | No | Seconds between ping-pong messages to the WebSocket server. Default is `29`. Acceptable only for `ws` connection. |
| `flushTimeoutMs` | `number` | No | Seconds between ping-pong messages to the WebSocket server. Default is `20`. Acceptable only for `sse` connection. |
8 changes: 4 additions & 4 deletions docs/photoniq/event-delivery/clients/listeners-and-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ The listener can be attached to the client when use `connect` method to Event De
let globalListener = function(event) {
if (event.type === "open") {
console.log('EDS connection established: ', event);
} else if (event.type === "connection-id") {
console.log("EDS assigned connection's ID: " + event.data);
} 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") {
Expand All @@ -32,7 +32,7 @@ let globalListener = function(event) {
}
};

let connection = PhotoniqEdsWs.connect({
let connection = PhotoniqEdsSdk.connect({
host: "<YOUR-PHOTONIQ>.photoniq.macrometa.io",
customerId: "<YOUR-CUSTOMER-ID>",
apiKey: "<YOUR-API-KEY>",
Expand Down Expand Up @@ -61,7 +61,7 @@ The `type` property can have the next `EDSEventType` enum values:
| **Type** | **Description** |
|----------------------|---------------------------|
| `open` | Openned WebSocket/SSE connection |
| `connection-id` | Received a connection ID |
| `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 |
Expand Down
28 changes: 28 additions & 0 deletions docs/photoniq/event-delivery/clients/query-set-and-batch.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ querySet.retrieve("SELECT * FROM <YOUR-COLLECTION> WHERE key=<YOUR-KEY>", (event
})
```

| **Argument** | **Type** | **Requred** | **Description** |
|--------------|------------|----------|------------------------|
| `query` | `string` | Yes | SQL query to retrieve/listen |
| `resultListener`| `function` | Yes | [Info](/photoniq/event-delivery/clients/listeners-and-errors#result-listener) |
| `errorListenerOrOptions`| `function `\|` QueryOptions` | No | [Info](/photoniq/event-delivery/clients/listeners-and-errors#error-listener) or set query options |
| `options` | `QueryOptions` | No | Set query options. |

### retrieveAndSubscribe

Retrieves initial data and subscribes to changes in the query:
Expand All @@ -30,6 +37,13 @@ querySet.retrieveAndSubscribe("SELECT * FROM <YOUR-COLLECTION> WHERE key=<YOUR-K
})
```

| **Argument** | **Type** | **Requred** | **Description** |
|--------------|------------|----------|-------------------|
| `query` | `string` | Yes | SQL query to retrieve/listen |
| `resultListener`| `function` | Yes | [Info](/photoniq/event-delivery/clients/listeners-and-errors#result-listener) |
| `errorListenerOrOptions`| `function `\|` QueryOptions` | No | [Info](/photoniq/event-delivery/clients/listeners-and-errors#error-listener) or set query options |
| `options` | `QueryOptions` | No | Set query options. |

### subscribe

Only subscribes to changes in the query:
Expand All @@ -39,6 +53,13 @@ querySet.subscribe("SELECT * FROM <YOUR-COLLECTION> WHERE key=<YOUR-KEY>", (even
})
```

| **Argument** | **Type** | **Requred** | **Description** |
|--------------|------------|----------|-------------------|
| `query` | `string` | Yes | SQL query to retrieve/listen |
| `resultListener`| `function` | Yes | [Info](/photoniq/event-delivery/clients/listeners-and-errors#result-listener) |
| `errorListenerOrOptions`| `function `\|` QueryOptions` | No | [Info](/photoniq/event-delivery/clients/listeners-and-errors#error-listener) or set query options |
| `options` | `QueryOptions` | No | Set query options. |

### unubscribe

Removes a subscription if the query was subscribed in the `QuerySet`. This applies only to the `retrieveAndSubscribe` and `subscribe` methods:
Expand Down Expand Up @@ -72,3 +93,10 @@ Removes all subscriptions in the `QuerySet`:
```js
querySet.unsubscribeAll();
```


#### `QueryOptions` instance schema:

| **Property** | **Type** | **Requred** | **Description** |
|--------------|----------|-------------|-----------------|
| `compress` | `bolean` | No | compress response data |
1 change: 1 addition & 0 deletions static/download/eds-clients/photoniq-eds-sdk.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 4bdb17a

Please sign in to comment.