StreamLink.NET
How to subscribe to multiple subjects

The StreamLink API supports subscriptions to multiple subjects using a single connection to the server. So there is no need to create multiple Caplin.StreamLink.StreamLink instances in order to make multiple requests.

The following example shows how to publish data to the Liberator. First it shows a class MyRecordSubscriptionListener that that implements the Caplin.StreamLink.Command.ICommandListener interface so that StreamLink can return the result of the publication request. Then it shows code that publishes to the Liberator a "Bid" value and an "Ask" value for the subject "/SubjectToPublishOn". The main steps in this code are:

  1. Create a StreamLink object and connect to the Liberator with the username "admin" and the password "admin".
  2. Populate a dictionary for field-value pairs. This defines the fields that will be published to for the given subject.
  3. Create a Caplin.StreamLink.Command.IPublishParameters object containing these field-value pairs.
  4. Call the Publish() method to publish these values to the Liberator."
  5. Sleep for a short period to ensure that the response has been received, through a call to MyRecordSubscriptionListener.SubscriptionStatusUpdated, or perhaps to SubscriptionErrorReceived().
  6. Disconnect from the Liberator.
CopyC#
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

using Caplin.StreamLink;
using Caplin.StreamLink.Subscription.Record;
using Caplin.StreamLink.Protocol.Rttp;
using Caplin.StreamLink.Logging;
using Caplin.StreamLink.Authentication;
using Caplin.StreamLink.Subscription;
using System.Diagnostics;
using Caplin.StreamLink.Configuration;

namespace StreamLinkExamples.Record
{
    public class MultipleSubscriptionsFull
    {
        // subscription listener class to be informed about all subscription information
        class MyRecordSubscriptionListener : IRecordSubscriptionListener
        {
            #region IRecordSubscriptionListener Members

            public void RecordUpdated(ISubscription subscription, IRecordEvent ev)
            {
                // simply printout all fields updated in this callback
                StringBuilder sb = new StringBuilder();

                sb.Append("Received update for " + ev.Subject + " [");
                foreach (IField field in ev.Fields.Values)
                {
                    sb.Append(field.Name + "=" + field.Value + ", ");
                }
                sb.Append("]");

                Console.WriteLine(sb.ToString());
            }

            public void RecordType2Updated(ISubscription subscription, IRecordType2Event ev)
            {
                // swallow
            }

            public void RecordType3Updated(ISubscription subscription, IRecordType3Event ev)
            {
                // swallow
            }

            #endregion

            #region ISubscriptionListener Members

            public void SubscriptionErrorReceived(ISubscription subscription, ISubscriptionErrorEvent ev)
            {
                // Process subscription error
                Console.WriteLine("Subscription Status Update " + ev.Subject + " Error: " + ev.Error.ToString());
            }

            public void SubscriptionStatusUpdated(ISubscription subscription, ISubscriptionStatusEvent status)
            {
                // Process subscription status callback
                Console.WriteLine("Subscription Status Update " + status.Subject + " Status: " + status.Status.ToString() + " " + status.Message);
            }

            #endregion
        }

        // Main execution method
        static void Example()
        {
            // Create basic streamlink connecting to host=localvm, port= 8080
            IStreamLinkConfiguration simpleConfiguration = SimpleConfiguration.CreateType2HttpConnection("liberatorhost.mydomain.com", 8080);
            IStreamLink myStreamLink = new StreamLink(simpleConfiguration);

            // log informational messages
            ConsoleLogger logger = new ConsoleLogger();
            logger.Level = SourceLevels.Information;
            myStreamLink.Logger = logger;

            // login using username=admin, password= admin
            myStreamLink.CredentialsProvider = new PasswordCredentialsProvider("admin", "admin");
            myStreamLink.StreamLinkProvider.Connect();

            // Create listener object
            IRecordSubscriptionListener listener = new MyRecordSubscriptionListener();

            // Create field parameters
            IRecordSubscriptionParameters parameters =
                myStreamLink.StreamLinkProvider.ParametersFactory.CreateRecordSubscriptionParameters(new string[] { 
                    "dFullName", "dTime", "dBestBid", "dBestAsk" });

            // define list of subjects (these are from the Demosrc datasource)
            string[] subjects = { "/DEMO/MSFT", "/DEMO/YHOO", "/DEMO/AAPL" };

            // create dictionary to keep track of subscriptions so we can unsubscribe later.
            IDictionary<string, IRecordSubscription> subscriptionMap = new Dictionary<string, IRecordSubscription>();

            // subscribe to all subjects using the same call back listener and parameters
            foreach (string subject in subjects)
            {
                IRecordSubscription mySubscription = myStreamLink.StreamLinkProvider.CreateRecordSubscription(listener,
                                                                                                          subject,
                                                                                                          parameters);
                subscriptionMap.Add(subject, mySubscription);
                mySubscription.Subscribe();
            }

            // Sleep so we see some updates being received.
            Thread.Sleep(20000);

            // finally unsubscribe to all subjects
            foreach (IRecordSubscription subscription in subscriptionMap.Values)
            {
                if (subscription.Status == SubscriptionStatus.Subscribed)
                {
                    subscription.Unsubscribe();
                }
            }

            myStreamLink.StreamLinkProvider.Disconnect();
        }

    }
}