WebSocket Subscriptions

Use WebSocket subscription to get real-time data from Orangepill

Listen for events on your Realm so your integration can automatically trigger reactions. Orangepill uses Websocket to notify your application when an event occures in your Realm. WebSockets are particularly useful for asynchronous events like when a new account is created, new transaction is processed or when incoming deposits are receieved.

Events

Events marked with * supported with Infobip extension enabled.

Event data

Each notification will include name of the event and id of the entity affected by event. If details of event are of your interest, use information from event data to retrieve entity details.

{
    "type": "notification", // event notification
    "event": "transaction.processed", // event name
    "data": "63967141191e9023f356df9f", // id of affected entity
    "timestamp": "162553625262" // Date.now()
}

Connect to WebSocket notification server using URL wss://stream.orangepill.cloud.

To subscribe for notification events use your Realm Key in message.

{ "type": "subscribe", "realm": <YOUR_REALM_KEY> }

You can unsubscribe from receiving notification events using similar message.

{ "type": "unsubscribe", "realm": <YOUR_REALM_KEY> }

In successful response to subscribe and unsubscribe events you will receive object.

{ "type":"info", "realm":"c9d4744c-0eae-4457-a665-2c18b9ac132b", "timestamp": 1671547687 }

If subscribe is not successful you will receive info and error objects.

{ "type":"info", "realm":"NONE", "timestamp": 1671547687 }
{ "type":"error", "realm":"Invalid Realm Key.", "timestamp": 1671547687 } }

Subscribe event response.

{ "type":"subscribe", "realm":"c9d4744c-0eae-4457-a665-2c18b9ac132b", "timestamp": 1671547687 } }

Unsubscribe event response.

{ "type":"unsubscribe", "realm":"c9d4744c-0eae-4457-a665-2c18b9ac132b", "timestamp": 1671547687 } }

Example using JavaScript

Use https://api.orangepill.cloud for live example of Event Stream Explorer.

// JavaScript example

const ws = new Sockette('wss://stream.orangepill.cloud', {
  timeout: 5e3,
  maxAttempts: 10,
  onopen: e => subscribe(),
  onmessage: e => message(e),
  onreconnect: e => {console.log('Reconnecting...'); subscribe();},
  onmaximum: e => {console.log('Stop Attempting!')},
  onclose: e => {console.log('Closed!'); cancelKeepAlive();},
  onerror: e => {console.log('Error:')}
});


function message(message) {

    var notification = JSON.parse(message.data);
    
    var event = notification.data.event;
    var data = notification.data.data;
    var timestamp = notification.data.timestamp;      

    // handle event with your logic
    switch (event) {
        case "account.created":
            // your logic here
        break;
        // ....
    }
}

function subscribe() {

    const realm = "<YOUR_REALM_KEY>";
    const obj = { "type": "subscribe", "realm": realm }
    ws.send(JSON.stringify(obj));
    keepAlive();

}

function unsubscribe() { 

    const realm = "<YOUR_REALM_KEY>";
    const obj = { "type": "unsubscribe", "realm": realm }
    ws.send(JSON.stringify(obj));
    
}

let timerId = 0; 

function keepAlive(timeout = 10000) { 
    ws.send('PING');  
    timerId = setTimeout(keepAlive, timeout);  
}

function cancelKeepAlive() {  
    if (timerId) {  
        clearTimeout(timerId);  
    }  
}

Last updated