The following example shows a how to subscribe to a record and receive updates.
The main execution method (Example()) shows how to subscribe to a record, and receive updates to it via MyRecordSubscriptionListener:
- Create an instance of Caplin.StreamLink.StreamLink (myStreamLink). The configuration is generated programatically.
- Set up a Caplin.StreamLink.Authentication.PasswordCredentialsProvider with the login information that Liberator needs.
- Establish a connection to the Liberator through a Connect() call on the StreamLink instance's IStreamLinkProvider.
- Create an instance of MyRecordSubscriptionListener.
- Create the parameters for the subscription (CreateRecordSubscriptionParameters()); in this case the "Bid", "Ask", and "Volume" fields of the record are required.
-
Create a record subscription (mySubscription) by calling the
CreateRecordSubscription() method of the
IStreamLinkProvider, supplying the method with:
- the MyRecordSubscriptionListener,
- the subject name ("Subject1") of the record being subscribed to,
- the record subscription parameters.
- To activate the subscription call its Subscribe() method.
- Sleep the thread so that updates can be received. StreamLink will pass updates to the application through a call to the RecordUpdated() method of MyRecordSubscriptionListener (assuming "Subject1" is a type 1 record).
- Finally disconnect from the Liberator by calling the IStreamLinkProvider's Disconnect() method.
CopyC#
using System.Threading; using System; using System.Diagnostics; 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 Caplin.StreamLink.Configuration; namespace StreamLinkExamples.Record { public class CreateRecordSubscriptionFull { // subscription listener class to be informed about all subscription information class MyRecordSubscriptionListener : IRecordSubscriptionListener { #region IRecordSubscriptionListener Members public void RecordUpdated(ISubscription subscription, IRecordEvent ev) { // Process update Console.WriteLine("Received update for " + ev.Subject + " Bid value is " + ev.Fields["Bid"].Value); Console.WriteLine("Received update for " + ev.Subject + " Ask value is " + ev.Fields["Ask"].Value); Console.WriteLine("Received update for " + ev.Subject + " Volume value is " + ev.Fields["Volume"].Value); } 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 IStreamLinkConfiguration myConfiguration = SimpleConfiguration.CreateType2HttpConnection("liberatorhost.mydomain.com", 8080); IStreamLink myStreamLink = new StreamLink(myConfiguration); 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[] { "Bid", "Ask", "Volume" }); IRecordSubscription mySubscription = myStreamLink.StreamLinkProvider.CreateRecordSubscription(listener, "Subject1", parameters); // Finally, subscribe mySubscription.Subscribe(); // Sleep so we see some updates being received. Thread.Sleep(20000); myStreamLink.StreamLinkProvider.Disconnect(); } } }