Server Event APIs (V9 ServicePack 2 or earlier)
IMPORTANT: The following APIs are applicable for version V9SP2 (or earlier) of Visualizer and Fabric. If you are using version V9SP3 (or later), refer to the latest Server Events APIs.
Server Events is a capability of the Quantum Fabric run-time server that lets the back-end services generate and subscribe to events. Server Events help you to generate events asynchronously such as processing a submitted order and invoking a time-taking activity where the client app does not need to wait for the response.
- The Server Events APIs are supported in Android, iOS, and SPA/DW platforms.
- Only one connection per application and one callback for event notification is allowed.
- Server Events are only supported for on-premise instances of Fabric.
Prerequisites
Enable the Server Event APIs on Visualizer. To enable the APIs, follow these steps:
- From the left navigation bar of Visualizer, select Project Settings.
- On the Project Settings window, in the Application section, select Enable Server Events APIs.
subscribeServerEvents API
The subscribeServerEvents API opens a connection and sends a subscription message for the topics you have provided. After subscribing, the application starts receiving the ServerEvents messages for the subscribed topics.
IMPORTANT:
Make sure that you subscribe to the topics by using this API when an app is launched because there is no support to reconnect to a topic if a connection fails or if the app crashes. For more information, refer to Limitations.
Syntax
KNYMobileFabric.subscribeServerEvents(topicsToSubscribe, subscribeOptions);
Parameters
Parameter | Type | Description |
---|---|---|
topicsToSubscribe | String | Specifies the events to be subscribed. |
subscribeOptions | JSON |
It determines the success and failure of the subscription. This parameter must contain the following functions:
|
Sample Code
var eventsToSubscribe = ["service1/operation1", "service1/operation2", "service2/operation1"]; subscribeOptions = { "onEventCallback": function(message) { //Handle the server event notification }, "onFailureCallback": function(error) { //Handle the subscription failure, majorly due to websocket failure. } }; KNYMobileFabric.subscribeServerEvents(eventsToSubscribe, subscribeOptions);
unsubscribeServerEvents API
The unsubscribeServerEvents API is used to unsubscribe the ServerEvents' messages for the topics you have provided.
NOTE:
If you unsubscribe from all the topics, make sure that you close the existing connection by using the closeConnection option.
Syntax
KNYMobileFabric.unsubscribeServerEvents(topicsToUnsubscribe, unsubscribeOptions);
Parameters
Parameter | Type | Description |
---|---|---|
topicsToUnsubscribe | String | Specifies the events to be unsubscribed. |
UnsubscribeOptions | JSON |
This parameter must contain the following functions: |
Sample Code
var eventsToUnsubscribe = ["service1/operation1", "service1/operation2", "service2/operation1"]; unsubscribeOptions = { "closeConnection": true, //pass this value as true if you want to close the existing connection. "onCloseCallback": function(error) { //callback will be invoked if closeConnection is set to true and websocket connection is closed successfully. } }; KNYMobileFabric.unSubscribeServerEvents(eventsToUnsubscribe, unsubscribeOptions);
publishServerEvents API
The publishServerEvents API publishes events to server from the client SDK API.
Syntax
KNYMobileFabric.publishServerEvents(eventsToPublish);
Parameters
Parameter | Type | Description |
---|---|---|
eventsToPublish | String | Specifies the events to be published. |
Sample Code
var eventsToPublish = [{ "topic": "transaction/deposit", "data": { "amount": "1500", "user": "clientevents", "account": "1000", "transaction": "deposit" } }];
KNYMobileFabric.publishServerEvents(eventsToPublish);
Frequently Asked Questions
- Can I subscribe to a topic more than once?
Yes, you can subscribe to events for a topic multiple times. The events will be notified each time an application has subscribed to that topic.
- Can I unsubscribe to a partial list of topics?
Yes, you can unsubscribe to a partial list of topics.
- Can I use different callbacks for every subscription?
Currently, the API maintains only one callback and the events for all topics will be notified through the same callback. When a different callback is passed with another subscription, all the events will start invoking the latest callback provided in the subscription.
Limitations
- There is no support to reconnect to a topic if a connection fails or if the client app crashes. Therefore, the app will not receive notifications when an event is invoked.
If the connection fails or if the app crashes, you need to use the subscribeServerEvents API to subscribe to the topics. Make sure that you call the API when the app is launched.