Subscribing to a Binary subject
The page provides an overview of how to subscribe to a subject of type Binary.
Requirements
To use the Binary data type, your Caplin components and libraries must be at least as recent as the versions listed in the table below:
| Product | Minimum Version |
|---|---|
C DataSource API |
8.0.12 |
Java DataSource API |
8.0.11 |
.NET DataSource API |
8.0.12 |
Liberator |
8.0.15 |
Transformer |
8.0.8 |
StreamLink Java |
8.0.7 |
StreamLink JS |
8.0.7 |
StreamLink .NET |
8.0.7 |
Overview
To subscribe to a Binary subject using StreamLink JS, follow the steps below:
-
Implement the
onBinaryUpdatemethod in your implementation of SubscriptionListener. The method receives an event object of type BinaryEvent. TheBinaryEvent.getBinary()method returns the binary object.var subscriptionListener = { ... onBinaryUpdate: function (subscription, event) { var binaryData = event.getBinary(); console.log(binaryData.toString()); } ... }; -
Subscribe to the Binary subject and connect
streamLink.subscribe("/EXAMPLES/BINARY/AAPL", subscriptionListener); streamLink.connect();
Example
The StreamLink Live Examples packaged with Liberator include an example of subscribing to binary data.
To view the live example, follow the steps below:
-
Deploy Liberator to a Deployment Framework
-
Activate Liberator’s LiberatorWebsite and LiberatorDemoDataSource built-in blades:
./dfw activate LiberatorWebsite LiberatorDemoDataSource
-
Start deployed components:
./dfw start
-
Open a web browser, and navigate to http://localhost:18080/docs/sljs/interactive/#tutorial/binary-subscription
The source code for the live example is reproduced below:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript"
src="http://localhost:18080/sljs/streamlink.js"></script>
</head>
<body onunload="streamLink.disconnect();">
<p>Binary:</p>
<pre id='display'>No Binary Data Received</pre>
<pre id='displayLog'>Log:</pre>
<script>
// Instantiate StreamLink.
var streamLink = caplin.streamlink.StreamLinkFactory.create({
username: "demouser",
password: "demopass",
liberator_urls: "rttp://localhost:18080"
});
// Define a subscription listener.
var binaryListener = {
onSubscriptionStatus: function (subscription, event) {
log(event.getSubject() + " is now " + event.getStatus());
},
onSubscriptionError: function (subscription, event) {
log("Error: Subject " + event.getSubject() + " is " + event.getError());
},
onBinaryUpdate: function (subscription, event) {
// onBinaryUpdate is called whenever a value in a binary object is changed.
render(event.getBinary().toString());
}
};
// Define a function to redraw the binary with all the data.
var binaryElement = document.getElementById('display');
function render(binaryString) {
binaryElement.innerHTML = binaryString;
}
// Define a function to write to the log.
var logArea = document.getElementById("displayLog");
function log(line) {
logArea.innerHTML += "\n" + line;
logArea.scrollTop = logArea.clientHeight;
}
// Subscribe and connect.
streamLink.subscribe("/EXAMPLES/BINARY/DELL", binaryListener);
streamLink.connect();
</script>
</body>
</html>
See also: