Client Development Tutorial

Step 3 - Setup a Subscription

In this step we will do the following:

Attaching the Event Handler

  1. Highlight the "Subscription" menu item, and then double-click it; this will create a new/empty event handler.

  2. The cursor will be automatically positioned to the first empty line. This is where you will start coding (next).

Creating the New Subscription

  1. First, we need to define our subscription object. Our use of this subscription in this tutorial will be very basic, meaning that we will simply create the subscription and assume that it will remain alive throughout the lifetime of the connection.

  2. Move the cursor above the event handler and copy/paste the following code:
    private Subscription m_subscription;

  3. Move the cursor back into the event handler and now define our subscription, but only if it has not already been defined:
    if( m_subscription == null )
    {
     m_subscription = new Subscription(m_session.DefaultSubscription);
     m_subscription.PublishingEnabled=true;
     m_subscription.PublishingInterval=1000;
     m_session.AddSubscription(m_subscription);
     m_subscription.Create();
    }

Adding the Monitored Item

  1. Now we will take the NodeId of the currently selected node, and will turn it into a monitored item which we will add to our subscription.

  2. Move the cursor above the event handler and create a monitored item variable:
    private MonitoredItem monitoredItem;

  3. Move the cursor back into the event handler and create the following "if block" below the code that you have previously created:
    if( monitoredItem == null )
    {
     monitoredItem = new MonitoredItem( m_subscription.DefaultItem );
      monitoredItem.StartNodeId =
      monitoredItem.AttributeId = Attributes.Value;
      monitoredItem.MonitoringMode = MonitoringMode.Reporting;
      monitoredItem.SamplingInterval = 1000;
      monitoredItem.QueueSize = 0;
      monitoredItem.DiscardOldest = true;
     // define event handler for this item, and then add to subscription
      monitoredItem.Notification += new MonitoredItemNotificationEventHandler( monitoredItem_Notification);
     m_subscription.AddItem( monitoredItem );
    }

Next, Step 4 - Consuming Data-changes.