Use the PubSub Application Programming Interface (API)
- Last UpdatedApr 08, 2026
- 2 minute read
When using a connection to PubSub from a Business Logic Tier (BLT) component, there are two choices: to use a global connection or to use a local connection. Both choices are valid; however, if a global connection is used, it must never be closed. The only clean-up required for a global connection is to unsubscribe from any topics that were subscribed while executing the component. It is important never to close the global PubSub connection, because it may still be in use by other threads.
A local connection should be closed after it is no longer required, and the act of closing it will automatically unsubscribe from any topics that were subscribed using the connection.
Example code 1
The following example code details the use of the PubSub global connection.
// Private attribute for the global connection.
private PubSubConnection _globalConnection = null;
// Method to check the connection state.
private void CheckConnection()
{
if (_globalConnection == null)
{
_globalConnection = PubSubConnection.GetGlobalPubSubConnection();
}
if (_globalConnection.IsConnected() == false)
{
_globalConnection.OpenConnection();
}
}
// Method to publish the data checking the global connection first.
public void SendData(string topic, string data)
{
CheckConnection();
_globalConnection.Publish(topic, data, PubSubHeaderData.PS_NORMAL_DATA, "", PubSubSecurityFlag.PS_WINDOWS);
}Example code 2
The following example code details the use of the local connection.
// Private attribute for the PubSub connection.
m_Connection = new PubSubConnection();
// Using the method to publish using the local connection.
m_Connection.Publish(topic, Data, HD, SecGroup, SecFlag);
// Method to clean up and disconnect from PubSub.
private void Cleanup()
{
if (m_Connection != null)
{
if (m_Connection.IsConnected())
{
m_Connection.Disconnect();
}
m_Connection.Dispose();
}
}