Scenario 9 - Realtime Data with WebSocket API¶
This is a tutorial that is part of a series and can be used as a standalone tutorial on how to work with real time data with a WebSocket API. For more details on the scenario and general prerequisites, please see the scenario overview page.
Time to Complete : 5 minutes
Quantis wants to provide real time location of their trains to their customers. The sensors in the train will be providing real time events and Quantis wants to convert it to WebSockets and expose it as a Streaming API so that the client mobile applications can subscribe to it and receive real time events.
WSO2 Streaming Integrator(SI) is a streaming data processing server that integrates streaming data and takes action based on streaming data. Quantis is planning to use Streaming Integrator to process the real time event data.
Step 1: Develop a service in Streaming Integrator¶
To develop a service in Streaming Integrator, you need to use Streaming Integrator tooling. Siddhi is the Event Processing Engine that is used inside the Streaming Integrator. To process the WebSockets you can write the service in Siddhi Query Language. You can test the service in the tooling itself. Once the development is complete you can export it as a Siddhi app and add it to the Streaming Integrator runtime.
Here, for simplicity, the service is already created and exported as a SiddhiApp and added to the Streaming Integrator instance in the tutorial setup. You can test the Streaming Integrator service by invoking it directly. You will start receiving Realtime events by connecting a WebSocket client (you can use wscat).
wscat -c ws://localhost:8025/
Step 2: Expose the WebSocket via API Manager¶
Once you expose the events via a WebSocket Server, you can expose the WebSockets with API Manager as you do with other APIs where you can provide secure access, Rate limiting, Throttling, Monetization, Analytics etc. The API is already published to the Developer Portal. To invoke the API, you can follow the below steps.
- To subscribe to the WebSocket API, go to https://localhost:9443/devportal/ Developer Portal and select Quantis tenant domain. (You need to log out from your previous tenant). This will redirect you to the Quantis Developer Portal.
- Sign in with a Quantis, Developer Portal user. Use user as
[email protected]and password as
TrainRealLocationAPIand click subscribe using a policy and generate the access token.
You can use the above fetched access token to subscribe to the location of the train 456 (Topic : loc-train-qnt-456), using a WebSocket client. For example you can use wscat tool, subscribe as below.
wscat -c ws://localhost:9099/t/quantis.com/train/location/1.0.0/loc-train-qnt-456/ -H "Authorization: Bearer <Access Token>"
Try out the next scenario in the series, Notifications Using WebHooks.Top