Sending Data to the Liberator

Let's take a look at an example of how a client application can use StreamLink to modify data on a Liberator server. In this case the application creates a new record data item. This pattern is used for issuing all StreamLink commands.

 

1. Get an instance of the StreamLink class

The application will issue a subscription request to StreamLink, so the first thing to do is implement a SubscriptionListener that receives the updates resulting from the subscription. In the pseudo code fragments below the record subscription listener implementation is called MySubscriptionListener. So, let's get an instance of the StreamLink class using:

StreamLink streamLink = StreamLinkFactory.create({
                           liberator_urls: "rttp://liberator1:8080",
                           username: "demouser",
                           password: "demopass"
                           });

In StreamLink, there are variants of the constructor that enable you to obtain configuration from a properties file. The create() method also lets you set less common configuration options by passing in an optional StreamLinkConfiguration object.

 

2. Set up the command listener

Next, we set up the command listener using:

// Create an instance of a class that implements CommandListener.
myCommandListener = new MyCommandListener();

 

3. Create the fields

Create the fields that you want to publish. In JavaScript, you can represent these in object literal notation:

fields = {"dBestBid": "1.2345", "dBestAsk": "1.2466"};

 

4. Command StreamLink to send the fields

Issue a “Publish To Subject” command to streamLink to send the fields. Assuming the application is already connected to the Liberator, StreamLink sends the command immediately:

streamLink.publishToSubject("/MyRecord", fields, myCommandListener);

When the command has executed successfully, StreamLink calls the onCommandOk() method of myCommandListener (instantiation of MyCommandListener). If the command fails, for example because the user does not have permission to create record subjects on Liberator, StreamLink calls onCommandError() instead.