UNPKG

@nuralogix.ai/dfx-api-client

Version:

DeepAffex API JavaScript Client Library

67 lines (57 loc) 2.46 kB
[@nuralogix.ai/dfx-api-client](../index.md) / [WebSocket](index.md) / events There are three WebSocket events: * `onmessage` * `onclose` * `onerror` ### onmessage `onmessage` is emitted when a new response is sent from the server to the client. You can have your custom logic based on each `actionId` you are receiving as part of the response. ```js const apiClient = client(); apiClient.websocket.onmessage = (requestId: string, status: string, message: any, actionId: ActionIdValues) => { // 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. // When authenticated, a reponse with a '0718' actionId will be returned from the server. // Once received, you can subscribe to measurement results by sending a '0510' actionId. if (actionId === ActionId.ORGANIZATIONS.LOGIN_WITH_TOKEN) { apiClient.websocket.sendMessage( ActionId.MEASUREMENTS.SUBSCRIBE_RESULTS, { Params: {ID: 'your-measurement-id-goes-here' }, RequestID: '' } ); } // A '0510' actionId will be returned from the server when // are subscribed to measurement results. if (actionId === ActionId.MEASUREMENTS.SUBSCRIBE_RESULTS) { if (Object.keys(message).length === 0) { // If you received an empty message then it means your // successfully subscribed to the measurement results. // here you can have your logic to start your measurement } else { // Otherwise, the measurement is already started and the // message contains measurement results. You can process/display results. } } // A 0506 actionId is emitted on each chunk number if (actionId === ActionId.MEASUREMENTS.DATA) { console.log('ChunkOrder', message.ChunkOrder) } } ``` ### onclose `onclose` [CloseEvent](https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent) is emitted when the connection is closed. ```js const apiClient = client(); apiClient.websocket.onclose = (e: CloseEvent) => { console.log('connection closed!', e.code, e.reason, e.wasClean); } ``` ### onerror `onerror` [Event](https://developer.mozilla.org/en-US/docs/Web/API/Event) is emitted when there is an error. ```js const apiClient = client(); apiClient.websocket.onerror = (e: Event) => { console.log('error', e); } ```