Below example provides insight into the Streaming mechanism detailed in OpenAPI Streaming page and describes how to perform below activities:
- How to make a streaming connection.
- Adding a new subscription.
- Removing the subscription.
On clicking "Connect" button, application makes a streaming connection with the server. As soon as streaming connection is made, contextId is shown and "Subscribe" button is enabled. This is the code, which makes a streaming connection.
Once we have connection established, "Subscribe" button becomes visible. On clicking "Subscribe" button, one can make the subscription. In this example, we have made subscription to the "Post /openapi/trade/v1/infoprices/subscriptions" endpoint. In the response, a snapshot is returned which is shown in "Snapshot" div and streamed data from the server is shown in the "Messages" div. Below is the code snippet, which makes subscription.
Handling Snapshot and stream data:
Below code block depicts how to handle snapshot and updates:
To delete the active subscription, click on the "Unsubscribe" button. On deleting subscription, the server stops streaming data to the client so there is no update on the screen. Below is the code which delete subscription. As we can see it is a simple delete call to the subscription server with contextId and referenceid in the Url as route parameters.