The OSGi Events WebSocket is an event subscription system based on the Bosch IoT Gateway Software WebSockets Server. It is implemented as a WebSocket Application, which contains all the server-side logic.
To use the OSGi Events WebSocket Application connect to the Event Application URI via:
ws://<osgi_host>:<port>/<application_uri>
where <application_uri> can be:
Subscribing and Unsubscribing with Message
To subscribe for additional events, a text messages contain a JSON Object can be used. The JSON Object contains:
Every property must be in lower-case.
The OSGi Events WebSocket Application supports Subscribe and Unsubscribe.
The Subscribing and Unsubscribing with Message approach is recommended over the Legacy Subscribing with URI approach.
Subscribe
The subscription format is as follows:
Single topic subscription without a filter:
//Without userkey
{ "command": "subscribe", "params": "someTopic"}
//With userkey
{ "command": "subscribe", "userKey": "someKey", "params": "someTopic"}
Multiple topic subscription without a filter:
//Without userkey
{ "command": "subscribe", "params":[ "topicOne", "topicTwo", "topicThree"]}
//With userkey
{ "command": "subscribe", "userKey": "someKey", "params":[ "topicOne", "topicTwo", "topicThree"]}
One or more topic(s) with or without a filter:
//Every JSON object in the params array contains information for one subscription. It can have one or more topic(s) and one optional filter
//Without userkey
{ "command": "subscribe", "params":[{ "filter": "oneFilter", "topic": "oneTopic"}, { "filter": "oneFilter", "topic":[ "one", "two", "three"]}, { "topic": [ "four", "five"]}]}
//With userkey
{ "command": "subscribe", "userKey": "someKey", "params":[{ "filter": "oneFilter", "topic": "oneTopic"}, { "filter": "oneFilter", "topic":[ "one", "two", "three"]}, { "topic": [ "four", "five"]}]}
Unsubscribe
The unsubscribe format is as follows:
Once you send the unsubscribe command you will get a response from the server with the successful and unsuccessful cancellations.
Subscription cancelling:
//Single subscription cancelling
{ "command": "unsubscribe", "userkey": "someKey", "params": "someId"}
//Multiple subscriptions cancelling
{ "command": "unsubscribe", "userkey": "someKey", "params":[ "idOne", "idTwo", "idThree", "idFour", "idFive"]}
Response after cancelling subscription:
//Here subscriptions with idOne, idThree and idFive have been cancelled successfully while subscription with idTwo and idFour have failed
{ "unsuccessful":[ "idTwo", "idFour"], "successful":[ "idOne", "idThree", "idFive"], "userkey": "someKey"}
Additionally if you have subscribed with an URI, you can unsubscribe easily by putting first as the params value:
Unsubscribing from the first subscription made from the URI:
{"command": "unsubscribe", "params": "first"}
Subscribing with URI (Deprecated)
This approach is deprecated, so Subscribing and Unsubscribing with Message approach described above is the recommended one.
To subscribe for events provide the subscription information after the application URI, which can be:
ws://<osgi_host>:<port>/<application_uri>/<event-topic>
ws://<osgi_host>:<port>/<application_uri>/<event-topic>/?filter=<event-filter>
ws://<osgi_host>:<port>/<application_uri>/?filter=<event-filter>&topic=someTopic
ws://<osgi_host>:<port>/<application_uri>/?topics=<event-topic1>,<event-topic2>,<event-topic3>&filter=<event-filter>
ws://<osgi_host>:<port>/<application_uri>/?topics=<event-topic1>,<event-topic2>
Error Codes
If the events protocol is not followed the webSocket connection will be closed. When the webSocket connection is closed, a closeFrame will be sent with a code and a reason. The following codes are defined for the events subprotocol:
Code |
Reason |
|---|---|
4001 |
Expired subscription. |
4002 |
Invalid URI query format, must begin with filter, topic or topics. |
4003 |
Invalid URI format, must have a single filter. |
4004 |
Invalid format, no topic found. |
4005 |
Invalid format, received empty topic. |
4006 |
Invalid JSON array or object format. |
4007 |
Invalid JSON format, received an empty array. |
4008 |
Invalid JSON format, received an invalid property, must be either filter or topic. |
4009 |
Invalid JSON format, every JSON object must contain a topic. |
4010 |
Invalid JSON format, received multiple topics. |
4011 |
Invalid JSON topic format, the topic array must contain only String objects. |
4101 |
Unsupported command. |
4102 |
Command not found in the JSON object. |
4103 |
Invalid subscription message format, must be JSON object. |
4104 |
Invalid subscription message format, illegal property in the JSON object. |
4105 |
Missing params property in the subscription JSON object. |
4106 |
Invalid params property format, must be String, String[], JSONObject or JSONArray. |
4107 |
Invalid params property format, the JSONArray must contain only String objects or JSONObjects. |
4108 |
Subscription error. |
4109 |
Invalid subscriptionId format, must be String or String[]. |
4110 |
Missing subscriptionId, must be String or String[]. |
4111 |
Error, unsupported version, must be a Number, which is supported by the Remote Events subProtocol. |
4201 |
Received IllegalArgumentException. |
4202 |
Invalid serialization type, must be String or byte[]. |
4203 |
Exception occurred while sending text or data. |
Remote Events Push Notifications via WebSockets
For the new WebSocket/HTTP implementation the support for push notifications via WebSockets is provided by the following bundles and KitMan scripts:
Parameter |
Description |
KitMan Script |
|---|---|---|
com.prosyst.mbs.websockets.events.endpoint.jar |
Provides base implementation for Push Events WebSockets Endpoint, used by JSON-RPC and REST endpoints. |
|
com.prosyst.mbs.webservices.jsonrpc.event.websocket.endpoint.jar |
Registers JSON-RPC Push Events Subscription Endpoint. |
|
com.prosyst.mbs.webservices.rest.events.push.endpoint.jar |
Registers REST Push Events Subscription Endpoint. |
|
All of the above mentioned KitMan Scripts work both with the default and the legacy HTTP Service / Websockets implementations, as they contain the new and the old Push Events Websocket bundles.