> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/holoscan/sdk-user-guide/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.nvidia.com/holoscan/sdk-user-guide/_mcp/server.

# holoscan::PubSubContext

> Pub/Sub NetworkContext class for topic-based inter-fragment communication.

Pub/Sub [NetworkContext](networkcontext) class for topic-based inter-fragment communication.

`PubSubContext` provides topic-based publish/subscribe messaging for distributed Holoscan applications. It wraps the GXF `nvidia::gxf::PubSubContext` component, which is backend-agnostic and supports different discovery and transport implementations.

```cpp showLineNumbers={false}
#include <holoscan/pubsub_context.hpp>
```

**Inherits from:** `holoscan::gxf::GXFNetworkContext` (public)

---

## Constructors

### PubSubContext \[#pubsubcontext]

#### Move

inline

explicit

```cpp showLineNumbers={false}
template <typename ArgT,
          typename... ArgsT,
          typename = std::enable_if_t<   !std::is_base_of_v<::holoscan::NetworkContext, std::decay_t<ArgT>> &&   (std::is_same_v<::holoscan::Arg, std::decay_t<ArgT>> ||   std::is_same_v<::holoscan::ArgList, std::decay_t<ArgT>>)>>
holoscan::PubSubContext::PubSubContext(
    ArgT &&arg,
    ArgsT &&... args
)
```

#### Default

```cpp showLineNumbers={false}
holoscan::PubSubContext::PubSubContext() = default
```

---

## Methods

### gxf\_typename \[#gxftypename]

```cpp showLineNumbers={false}
const char * holoscan::PubSubContext::gxf_typename() const override
```

Get the type name of the GXF network context.

The returned string is the type name of the GXF network context and is used to create the GXF network context.

Example: "nvidia::holoscan::UcxContext"

**Returns:** The type name of the GXF network context.

### setup \[#setup]

```cpp showLineNumbers={false}
void holoscan::PubSubContext::setup(
    ComponentSpec &spec
) override
```

Define the network context specification.

**Parameters**

**`spec`** `ComponentSpec &`

The reference to the component specification.

---

### initialize \[#initialize]

```cpp showLineNumbers={false}
void holoscan::PubSubContext::initialize() override
```

Initialize the network context.

This function is called after the network context is created by holoscan::Fragment::make\_network\_context().

### clock \[#clock]

```cpp showLineNumbers={false}
std::shared_ptr<Clock> holoscan::PubSubContext::clock() override
```

Get the [Clock](clock) used by the network context.

**Returns:** The [Clock](clock) used by the network context, or nullptr if not applicable.

### get \[#get]

```cpp showLineNumbers={false}
nvidia::gxf::PubSubContext * holoscan::PubSubContext::get() const
```

Get the underlying GXF `PubSubContext` component.

**Returns:** Pointer to the GXF `PubSubContext`, or nullptr if not initialized.

### node\_name \[#nodename]

```cpp showLineNumbers={false}
std::string holoscan::PubSubContext::node_name() const
```

Get the node name for this context.

**Returns:** The node name string.

### get\_topics \[#gettopics]

```cpp showLineNumbers={false}
std::vector<nvidia::gxf::TopicInfo> holoscan::PubSubContext::get_topics() const
```

Get information about all known topics.

**Returns:** [Vector](../typedefs/vector) of TopicInfo structures.

### get\_publisher\_count \[#getpublishercount]

```cpp showLineNumbers={false}
size_t holoscan::PubSubContext::get_publisher_count(
    const std::string &topic
) const
```

Get number of publishers on a topic.

**Returns:** Number of publishers.

**Parameters**

**`topic`** `const std::string &`

Topic name to query.

---

### get\_subscriber\_count \[#getsubscribercount]

```cpp showLineNumbers={false}
size_t holoscan::PubSubContext::get_subscriber_count(
    const std::string &topic
) const
```

Get number of subscribers on a topic.

**Returns:** Number of subscribers.

**Parameters**

**`topic`** `const std::string &`

Topic name to query.

---

### registered\_publisher\_gids \[#registeredpublishergids]

```cpp showLineNumbers={false}
std::vector<nvidia::gxf::Gid> holoscan::PubSubContext::registered_publisher_gids() const
```

Get the GIDs of all locally registered publishers.

**Returns:** [Vector](../typedefs/vector) of publisher GIDs (snapshot under lock).

### registered\_subscriber\_gids \[#registeredsubscribergids]

```cpp showLineNumbers={false}
std::vector<nvidia::gxf::Gid> holoscan::PubSubContext::registered_subscriber_gids() const
```

Get the GIDs of all locally registered subscribers.

**Returns:** [Vector](../typedefs/vector) of subscriber GIDs (snapshot under lock).

### get\_publisher\_transmitter \[#getpublishertransmitter]

```cpp showLineNumbers={false}
nvidia::gxf::Expected<nvidia::gxf::Handle<nvidia::gxf::Transmitter>> holoscan::PubSubContext::get_publisher_transmitter(
    const nvidia::gxf::Gid &gid
) const
```

Look up the transmitter Handle for a registered publisher.

**Returns:** Handle to the [Transmitter](transmitter), or Unexpected with GXF\_ENTITY\_NOT\_FOUND.

**Parameters**

**`gid`** `const nvidia::gxf::Gid &`

Publisher GID.

---

### get\_subscriber\_receiver \[#getsubscriberreceiver]

```cpp showLineNumbers={false}
nvidia::gxf::Expected<nvidia::gxf::Handle<nvidia::gxf::Receiver>> holoscan::PubSubContext::get_subscriber_receiver(
    const nvidia::gxf::Gid &gid
) const
```

Look up the receiver Handle for a registered subscriber.

**Returns:** Handle to the [Receiver](receiver), or Unexpected with GXF\_ENTITY\_NOT\_FOUND.

**Parameters**

**`gid`** `const nvidia::gxf::Gid &`

Subscriber GID.

---

### to\_yaml\_node \[#toyamlnode]

```cpp showLineNumbers={false}
YAML::Node holoscan::PubSubContext::to_yaml_node() const override
```

Get a `YAML` representation of the network context.

**Returns:** `YAML` node including type, specs, resources of the network context in addition to the base component properties.

### reset\_backend\_objects \[#resetbackendobjects]

```cpp showLineNumbers={false}
void holoscan::PubSubContext::reset_backend_objects() override
```

Reset any backend-specific objects.

### set\_parameters \[#setparameters]

```cpp showLineNumbers={false}
void holoscan::PubSubContext::set_parameters() override
```

Set the parameters based on defaults (sets GXF parameters for GXF operators).

### id \[#id]

#### Mutable

```cpp showLineNumbers={false}
NetworkContext & holoscan::NetworkContext::id(
    int64_t id
)
```

Set the [NetworkContext](networkcontext) ID.

**Returns:** The reference to this network context.

**Parameters**

**`id`** `int64_t`

The ID of the network context.

---

#### Const

const

```cpp showLineNumbers={false}
int64_t holoscan::ComponentBase::id() const
```

Get the identifier of the component.

By default, the identifier is set to -1. It is set to a valid value when the component is initialized.

With the default executor (GXFExecutor), the identifier is set to the GXF component ID.

**Returns:** The identifier of the component.

### name \[#name]

#### Set the name of the network context (1)

```cpp showLineNumbers={false}
NetworkContext & holoscan::NetworkContext::name(
    const std::string &name
) &
```

Set the name of the network context.

**Returns:** The reference to the network context.

**Parameters**

**`name`** `const std::string &`

The name of the network context.

---

#### Set the name of the network context (2)

```cpp showLineNumbers={false}
NetworkContext && holoscan::NetworkContext::name(
    const std::string &name
) &&
```

Set the name of the network context.

**Returns:** The reference to the network context.

**Parameters**

**`name`** `const std::string &`

The name of the network context.

---

#### Const

const

```cpp showLineNumbers={false}
const std::string & holoscan::ComponentBase::name() const
```

Get the name of the component.

**Returns:** The name of the component.

### fragment \[#fragment]

#### Set the fragment of the network context

```cpp showLineNumbers={false}
NetworkContext & holoscan::NetworkContext::fragment(
    Fragment *fragment
)
```

Set the fragment of the network context.

**Returns:** The reference to the network context.

**Parameters**

**`fragment`** `Fragment *`

The pointer to the fragment of the network context.

---

#### Get a pointer to \[Fragment]\(fragment) object

```cpp showLineNumbers={false}
Fragment * holoscan::ComponentBase::fragment()
```

Get a pointer to [Fragment](fragment) object.

**Returns:** The Pointer to [Fragment](fragment) object.

#### Const

const

```cpp showLineNumbers={false}
const Fragment * holoscan::ComponentBase::fragment() const
```

Get a const pointer to [Fragment](fragment) object.

**Returns:** The const pointer to [Fragment](fragment) object.

### spec \[#spec]

#### Overload 1

```cpp showLineNumbers={false}
NetworkContext & holoscan::NetworkContext::spec(
    const std::shared_ptr<ComponentSpec> &spec
)
```

Set the component specification to the network context.

**Returns:** The reference to the network context.

**Parameters**

**`spec`** `const std::shared_ptr<ComponentSpec> &`

The component specification.

---

#### Overload 2

```cpp showLineNumbers={false}
ComponentSpec * holoscan::NetworkContext::spec()
```

Get the component specification of the network context.

**Returns:** The pointer to the component specification.

### spec\_shared \[#specshared]

```cpp showLineNumbers={false}
std::shared_ptr<ComponentSpec> holoscan::NetworkContext::spec_shared()
```

Get the shared pointer to the component spec.

**Returns:** The shared pointer to the component spec.

### add\_arg \[#addarg]

#### Add a resource to the network context (1)

```cpp showLineNumbers={false}
void holoscan::NetworkContext::add_arg(
    const std::shared_ptr<Resource> &arg
)
```

Add a resource to the network context.

**Parameters**

**`arg`** `const std::shared_ptr<Resource> &`

The resource to add.

---

#### Add a resource to the network context (2)

```cpp showLineNumbers={false}
void holoscan::NetworkContext::add_arg(
    std::shared_ptr<Resource> &&arg
)
```

Add a resource to the network context.

**Parameters**

**`arg`** `std::shared_ptr<Resource> &&`

The resource to add.

---

#### Add an argument to the component (1)

```cpp showLineNumbers={false}
void holoscan::ComponentBase::add_arg(
    const Arg &arg
)
```

Add an argument to the component.

**Parameters**

**`arg`** `const Arg &`

The argument to add.

---

#### Add an argument to the component (2)

```cpp showLineNumbers={false}
void holoscan::ComponentBase::add_arg(
    Arg &&arg
)
```

Add an argument to the component.

**Parameters**

**`arg`** `Arg &&`

The argument to add.

---

#### Add a list of arguments to the component (1)

```cpp showLineNumbers={false}
void holoscan::ComponentBase::add_arg(
    const ArgList &arg
)
```

Add a list of arguments to the component.

**Parameters**

**`arg`** `const ArgList &`

The list of arguments to add.

---

#### Add a list of arguments to the component (2)

```cpp showLineNumbers={false}
void holoscan::ComponentBase::add_arg(
    ArgList &&arg
)
```

Add a list of arguments to the component.

**Parameters**

**`arg`** `ArgList &&`

The list of arguments to add.

---

### resources \[#resources]

```cpp showLineNumbers={false}
std::unordered_map<std::string, std::shared_ptr<Resource>> & holoscan::NetworkContext::resources()
```

Get the resources of the network context.

**Returns:** The resources of the network context.

### args \[#args]

```cpp showLineNumbers={false}
std::vector<Arg> & holoscan::ComponentBase::args()
```

Get the list of arguments.

**Returns:** The vector of arguments.

### description \[#description]

```cpp showLineNumbers={false}
std::string holoscan::ComponentBase::description() const
```

Get a description of the component.

**Returns:** `YAML` string.

**See also:**
to\_yaml\_node()

### service \[#service]

```cpp showLineNumbers={false}
template <typename ServiceT = DefaultFragmentService>
std::shared_ptr<ServiceT> holoscan::ComponentBase::service(
    std::string_view id = ""
) const
```

Retrieve a registered fragment service or resource.

Retrieves a previously registered fragment service or resource by its type and optional identifier. Returns nullptr if no service/resource is found with the specified type and identifier.

Note that any changes to the service retrieval logic in this method should be synchronized with the implementation in `Fragment::service()` method to maintain consistency.

**Returns:** The shared pointer to the service/resource, or nullptr if not found or if type casting fails.

**Template parameters**

**`ServiceT`** `typename`

The type of the service/resource to retrieve. Must inherit from either [Resource](resource) or [FragmentService](fragmentservice). Defaults to [DefaultFragmentService](defaultfragmentservice) if not specified.

---

**Parameters**

**`id`** `std::string_view` — default: ""

The identifier of the service/resource. If empty, retrieves by type only.

---

### get\_service\_by\_type\_info \[#getservicebytypeinfo]

```cpp showLineNumbers={false}
std::shared_ptr<FragmentService> holoscan::ComponentBase::get_service_by_type_info(
    const std::type_info &service_type,
    std::string_view id = ""
) const
```

Retrieve a registered fragment service or resource for Python bindings.

This is a helper method for Python bindings to retrieve a service by its C++ type info.

**Returns:** The shared pointer to the base service, or nullptr if not found.

**Parameters**

**`service_type`** `const std::type_info &`

The type info of the service/resource to retrieve.

---

**`id`** `std::string_view` — default: ""

The identifier of the service/resource. If empty, retrieves by type only.

---

### gxf\_context \[#gxfcontext]

#### Mutable

```cpp showLineNumbers={false}
void holoscan::gxf::GXFComponent::gxf_context(
    gxf_context_t gxf_context
)
```

#### Const

const

```cpp showLineNumbers={false}
gxf_context_t holoscan::gxf::GXFComponent::gxf_context() const
```

### gxf\_eid \[#gxfeid]

#### Mutable

```cpp showLineNumbers={false}
void holoscan::gxf::GXFComponent::gxf_eid(
    gxf_uid_t gxf_eid
)
```

#### Const

const

```cpp showLineNumbers={false}
gxf_uid_t holoscan::gxf::GXFComponent::gxf_eid() const
```

### gxf\_tid \[#gxftid]

#### Mutable

```cpp showLineNumbers={false}
void holoscan::gxf::GXFComponent::gxf_tid(
    gxf_tid_t gxf_tid
)
```

#### Const

const

```cpp showLineNumbers={false}
gxf_tid_t holoscan::gxf::GXFComponent::gxf_tid() const
```

### gxf\_cid \[#gxfcid]

#### Mutable

```cpp showLineNumbers={false}
void holoscan::gxf::GXFComponent::gxf_cid(
    gxf_uid_t gxf_cid
)
```

#### Const

const

```cpp showLineNumbers={false}
gxf_uid_t holoscan::gxf::GXFComponent::gxf_cid() const
```

### gxf\_cname \[#gxfcname]

#### Overload 1

```cpp showLineNumbers={false}
std::string & holoscan::gxf::GXFComponent::gxf_cname()
```

#### Overload 2

```cpp showLineNumbers={false}
void holoscan::gxf::GXFComponent::gxf_cname(
    const std::string &name
)
```

### gxf\_graph\_entity \[#gxfgraphentity]

#### Overload 1

```cpp showLineNumbers={false}
std::shared_ptr<nvidia::gxf::GraphEntity> holoscan::gxf::GXFComponent::gxf_graph_entity()
```

#### Overload 2

```cpp showLineNumbers={false}
void holoscan::gxf::GXFComponent::gxf_graph_entity(
    std::shared_ptr<nvidia::gxf::GraphEntity> graph_entity
)
```

### gxf\_entity\_group\_name \[#gxfentitygroupname]

```cpp showLineNumbers={false}
std::string holoscan::gxf::GXFComponent::gxf_entity_group_name()
```

The name of the entity group this component belongs to.

### gxf\_entity\_group\_id \[#gxfentitygroupid]

```cpp showLineNumbers={false}
gxf_uid_t holoscan::gxf::GXFComponent::gxf_entity_group_id()
```

The group id of the entity group this component belongs to.

### gxf\_cptr \[#gxfcptr]

```cpp showLineNumbers={false}
void * holoscan::gxf::GXFComponent::gxf_cptr()
```

### gxf\_component \[#gxfcomponent]

```cpp showLineNumbers={false}
nvidia::gxf::Handle<nvidia::gxf::Component> holoscan::gxf::GXFComponent::gxf_component()
```

### gxf\_initialize \[#gxfinitialize]

```cpp showLineNumbers={false}
void holoscan::gxf::GXFComponent::gxf_initialize()
```

### set\_gxf\_parameter \[#setgxfparameter]

```cpp showLineNumbers={false}
void holoscan::gxf::GXFComponent::set_gxf_parameter(
    const std::string &component_name,
    const std::string &key,
    ParameterWrapper &param_wrap
)
```

Set a given parameter on the underlying GXF component.

### reset\_gxf\_graph\_entity \[#resetgxfgraphentity]

```cpp showLineNumbers={false}
void holoscan::gxf::GXFComponent::reset_gxf_graph_entity()
```

### setup\_backend \[#setupbackend]

```cpp showLineNumbers={false}
virtual void holoscan::PubSubContext::setup_backend()
```

Set up the PubSub backend (discovery, transport, serializer).

Called automatically from initialize() after the GXF component is created and `get()` returns a valid pointer.

The default implementation is a no-op. Subclasses should override this to create backend components and inject them into the GXF `PubSubContext`:

**Example**

```cpp showLineNumbers={false}
void setup_backend() override {
  auto* gxf_ctx = get();
  gxf_ctx->set_discovery(std::make_shared<MyDiscovery>(...));
  gxf_ctx->set_transport(std::make_shared<MyTransport>(...));
  gxf_ctx->set_serializer(std::make_shared<MySerializer>(...));
  gxf_ctx->initialize();
  gxf_ctx->init_context();
}
```

**See also:**
DDSPubSubNetworkContext for the FastDDS backend implementation.

### update\_params\_from\_args \[#updateparamsfromargs]

#### Update parameters based on the specified arguments

```cpp showLineNumbers={false}
void holoscan::Component::update_params_from_args()
```

Update parameters based on the specified arguments.

#### Update parameters based on the specified arguments (with params)

```cpp showLineNumbers={false}
void holoscan::ComponentBase::update_params_from_args(
    std::unordered_map<std::string, ParameterWrapper> &params
)
```

Update parameters based on the specified arguments.

### service\_provider \[#serviceprovider]

```cpp showLineNumbers={false}
void holoscan::ComponentBase::service_provider(
    FragmentServiceProvider *provider
)
```

Set the service provider that owns this component.

---

## Static methods

### register\_converter \[#registerconverter]

```cpp showLineNumbers={false}
template <typename typeT>
static void holoscan::ComponentBase::register_converter()
```

Register the argument setter for the given type.

If an operator or resource has an argument with a custom type, the argument setter must be registered using this method.

The argument setter is used to set the value of the argument from the `YAML` configuration.

This method can be called in the initialization phase of the operator/resource (e.g., `initialize()`). The example below shows how to register the argument setter for the custom type (`Vec3`):

It is assumed that `YAML::convert<T>::encode` and `YAML::convert<T>::decode` are implemented for the given type. You need to specialize the `YAML::convert<>` template class.

For example, suppose that you had a `Vec3` class with the following members:

You can define the `YAML::convert<Vec3>` as follows in a '.cpp' file:

Please refer to the [yaml-cpp documentation](https://github.com/jbeder/yaml-cpp/wiki/Tutorial#converting-tofrom-native-data-types) for more details.

**Template parameters**

**`typeT`** `typename`

The type of the argument to register.

---

**Example**

```cpp showLineNumbers={false}
void MyOp::initialize() {
  register_converter<Vec3>();
}
```

**Example**

```cpp showLineNumbers={false}
struct Vec3 {
  // make sure you have overloaded operator==() for the comparison
  double x, y, z;
};
```

**Example**

```cpp showLineNumbers={false}
namespace YAML {
template<>
struct convert<Vec3> {
  static Node encode(const Vec3& rhs) {
    Node node;
    node.push_back(rhs.x);
    node.push_back(rhs.y);
    node.push_back(rhs.z);
    return node;
  }

  static bool decode(const Node& node, Vec3& rhs) {
    if(!node.IsSequence() || node.size() != 3) {
      return false;
    }

    rhs.x = node[0].as<double>();
    rhs.y = node[1].as<double>();
    rhs.z = node[2].as<double>();
    return true;
  }
};
}
```

### register\_argument\_setter \[#registerargumentsetter]

```cpp showLineNumbers={false}
template <typename typeT>
void holoscan::ComponentBase::register_argument_setter()
```

Register the argument setter for the given type.

Please refer to the documentation of `register_converter()` for more details.

**Template parameters**

**`typeT`** `typename`

The type of the argument to register.

---

---

## Member variables

| Name                | Type                                                             | Description                                       |
| ------------------- | ---------------------------------------------------------------- | ------------------------------------------------- |
| `node_name_`        | `Parameter< std::string >`                                       |                                                   |
| `clock_`            | `Parameter< std::shared_ptr< gxf::Clock > >`                     |                                                   |
| `resources_`        | `std::unordered_map< std::string, std::shared_ptr< Resource > >` | The resources used by the network context.        |
| `spec_`             | `std::shared_ptr< ComponentSpec >`                               | The component specification.                      |
| `id_`               | `int64_t`                                                        | The ID of the component.                          |
| `name_`             | `std::string`                                                    | Name of the component.                            |
| `fragment_`         | `Fragment *`                                                     | Pointer to the fragment that owns this component. |
| `args_`             | `std::vector< Arg >`                                             | List of arguments.                                |
| `service_provider_` | `FragmentServiceProvider *`                                      | Pointer to the service provider.                  |
| `gxf_context_`      | `gxf_context_t`                                                  |                                                   |
| `gxf_eid_`          | `gxf_uid_t`                                                      |                                                   |
| `gxf_tid_`          | `gxf_tid_t`                                                      |                                                   |
| `gxf_cid_`          | `gxf_uid_t`                                                      |                                                   |
| `gxf_graph_entity_` | `std::shared_ptr< nvidia::gxf::GraphEntity >`                    |                                                   |
| `gxf_cname_`        | `std::string`                                                    |                                                   |
| `gxf_component_`    | `nvidia::gxf::Handle< nvidia::gxf::Component >`                  |                                                   |
| `gxf_cptr_`         | `void *`                                                         |                                                   |