# Subscribing to a Binary subject

The page provides an overview of how to subscribe to a subject of type [Binary](../datasource/datasource-binary.md).

## Requirements

To use the [Binary data type](../datasource/datasource-binary.md), your Caplin components and libraries must be at least as recent as the versions listed in the table below:

**Product versions supporting the Binary data type**

| 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](https://docs.caplin.com/developer/api/streamlinkjs/latest/), follow the steps below:

1. Implement the `onBinaryUpdate` method in your implementation of [SubscriptionListener](https://docs.caplin.com/developer/api/streamlinkjs/latest/classes/SubscriptionListener.html). The method receives an event object of type [BinaryEvent](https://docs.caplin.com/developer/api/streamlinkjs/latest/classes/BinaryEvent.html). The `BinaryEvent.getBinary()` method returns the binary object.

   ```js
   var subscriptionListener = {
     ...
     onBinaryUpdate: function (subscription, event) {
       var binaryData = event.getBinary();
       console.log(binaryData.toString());
     }
     ...
   };
   ```
2. Subscribe to the Binary subject and connect

   ```js
   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:

1. Deploy Liberator to a Deployment Framework
2. Activate Liberator’s **LiberatorWebsite** and **LiberatorDemoDataSource** built-in blades:

   ```
   ./dfw activate LiberatorWebsite LiberatorDemoDataSource
   ```
3. Start deployed components:

   ```
   ./dfw start
   ```
4. 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:

**Example: subscribing to a Binary subject**

```html
<!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**:

* [Binary data type](../datasource/datasource-binary.md)
