Subscribe using a Data Access Layer (DAL) connection
- Last UpdatedApr 08, 2026
- 2 minute read
This scenario guides you through the steps required to subscribe to a topic using the Data Access Layer (DAL).
To identify the code associated with each step, see the code comments in the Example code sections.
To subscribe to a topic using the DAL
- Open the Program.cs file from the created project.
- Add the following private global variables that will be processed in the callback:
- AutoResetEvent waitHandle = new AutoResetEvent(false);
- Create the status connection callback method to process the status of the connection call.
- Create the subscribe callback method to process the subscription call by doing the following:
- Handle the output of the data returned by the subscription if there are no errors.
- Set the state of the event to signal after processing the subscription callback.
- Verify that the DAL service has initialized.
- Get the global connection.
- Block the current thread until the current instance receives a signal or the timeout expires.
- Reset the global AutoResetEvent waitHandle variable declared out of scope.
- Subscribe to a topic with the callback that will process the subscription.
- Block the current thread until the current instance receives the subscription callback signal or the timeout expires.
- Unsubscribe from the topic.
Example code
The following example code details creating the status connection callback.
private void StatusConnectionCallback(BaseMessage response)
{
switch (response.MessageType)
{
case DALMessageType.ConnectionDisconnectedNotification:
Console.WriteLine("Subscriber:Connection disconnected from the driver.");
break;
case DALMessageType.ConnectionEstablishedNotification:
Console.WriteLine("Subscriber:Connection established with the driver.");
break;
case DALMessageType.DataSourceConnectionNotification:
Console.WriteLine("Subscriber:Connection established with pubsub.");
waitHandle.Set();
break;
case DALMessageType.ConnectionTimeoutNotification:
Console.WriteLine("Subscriber:Connection timed out.");
break;
default:
Console.WriteLine("Subscriber:Unknown Connection notification: " + response.MessageType.ToString());
break;
}
}The following example code details creating the subscription callback.
private void SubscriptionCallback(String topic, PubSubStatus psStatus, DALResponseStatus dalResponseStatus, PubSubHeaderData psHeaderData, PubSubBuffer dataBuffer, Object clientObject, String failureReason)
{
if (psStatus == PubSubStatus.PS_OK)
{
HandleTopicMessage(dataBuffer);
}
}
private void HandleTopicMessage(PubSubBuffer dataBuffer)
{
if (dataBuffer.GetNextItemType() == PubSubBuffer.PSDataType.PSDdouble)
{
object[] objValues = null;
while (dataBuffer.MoreItemsToRead())
{
dataBuffer.Read(out objValues);
}
if (objValues != null)
{
Console.WriteLine(quot;Value read: {(Double)objValues[0]}");
}
}
waitHandle.Set();
}The following example code details subscribing to a topic.
public void AddSubscription()
{
try
{
// Verify that the DAL service has initialized.
if (DALService.IsInitialized == false)
{
DALService.Initialize();
}
var StatusConnectionDelegate = new DALConnectionStatusCallBackMethod(StatusConnectionCallback);
var SubscriptionDelegate = new DALPSConnection.DALPSSubscripionDelegate(SubscriptionCallback);
// Get the global connection.
var psSubConn = DALPSClient.GetGlobalConnection("common", StatusConnectionDelegate);
// Block the current thread until the current instance receives a signal or the timeout expires.
waitHandle.WaitOne(TimeSpan.FromSeconds(10));
if (psSubConn.IsConnected)
{
// Reset the global AutoResetEvent waitHandle variable declared out of scope.
waitHandle = new AutoResetEvent(false);
var topic = "es.realtime.db.analog.AutoCommandAnalog01.curval";
// Subscribe to a topic with the callback that will process the subscription.
psSubConn.Subscribe(topic, SubscriptionDelegate, PubSubUpdateFlag.PS_INTEGRITY, null);
// Block the current thread until the current instance receives the subscription callback signal or the timeout expires.
waitHandle.WaitOne(TimeSpan.FromSeconds(10));
// Unsubscribe from the topic.
psSubConn.UnSubscribe(topic, SubscriptionDelegate, null, null);
}
}
catch (Exception e)
{
Console.WriteLine("Error:" + e.Message);
}
}