@nuralogix.ai/dfx-api-client
Version:
DeepAffex API JavaScript Client Library
103 lines (88 loc) • 3.49 kB
Markdown
[@nuralogix.ai/dfx-api-client](../index.md) / [WebSocket](index.md) / methods
## Methods
There are three WebSocket methods:
* `connect`
* `disconnect`
* `sendMessage`
### connect
To establish a new WebSocket connection:
```js
const apiClient = client();
apiClient.websocket.connect();
```
`NOTE:` When socket connection is opened, the library tries to
authenticate against server using the `apiClient.session.deviceToken`
property that is previously stored in the current instance of the apiClient.
Please make sure the token is already set for the
current session before staring a connection.
### disconnect
To close the current connection:
```js
apiClient.websocket.disconnect();
```
### sendMessage
To send a message to the server:
```js
apiClient.websocket.sendMessage(actionId: ActionIdValues, data: any);
```
Example:
```js
/**
* Adds collected blood-flow data to a specific measurement. Upon submitting a
* chunk of data, the API will return a MeasurementDataID value representing
* the received chunk. Data must be sent to the server in the order produced by
* the DFX SDK. If a chunk it sent out of order, the server will return an error.
* Please ensure that new chunks are only sent after the server responds with a
* MeasurementDataID.
*
* Submitting measurements has three stages:
*
* a) starting,
*
* b) measurement body,
*
* c) closing a measurement.
*
* Each of these phases have the same payload structure however a different Action
* flag is to be sent with each request and must follow the CHUNK::ACTION format.
*
* Measurement Actions | Description
*
* FIRST::PROCESS | Start a new measurement (drop any existing), Process results
*
* FIRST::IGNORE | Start a new measurement (drop any existing), Do not process
*
* CHUNK::PROCESS | Arbitrary block of TOI data and process results
*
* CHUNK::IGNORE | Arbitrary block of TOI data and do not process results
*
* LAST::PROCESS | Finish a measurement cycle and process results
*
* LAST::IGNORE | Finish a measurement cycle and do not process
*
* `Payload` is binary data that can currently only be obtained by using our SDK.
* The Payload (binary content) field must be `base64-encoded`.
*
* Note: This endpoint is a subject to request throttling, you must not submit more
* data than can be obtained in real time. i.e., do not send more than five seconds
* of chunk data over the course of five seconds of real time.
*
* Response Error Codes Explanation:
*
* * "RATE_LIMIT": You have sent too many chunks in a given time period. See the Note above.
* * "MEASUREMENT_CLOSED": Requested Measurement is already finished. You need to create a new Measurement.
* * "MISALIGNED_CHUNK": Chunk Order was mismatched. i.e., ChunkOrder 2 was sent before ChunkOrder 1.
* * "INVALID_MEASUREMENT": Requested Measurement ID was not found.
* * "UNPACKER_RPC_ERROR": Payload validation has been failed. Reason(s) will be provided in error message.
*/
apiClient.websocket.sendMessage(
ActionId.MEASUREMENTS.DATA,
{
Params: {ID: 'your-measurement-id-goes-here' },
Action: 'set-action-here',
Payload: Buffer.from('your-payload').toString('base64')
}
);
```
`Note:` the same `actionId` you set here is returned when
the response of your call is received from the server in [onmessage](events.md#onmessage) event