> This page is for version 4.3.
> For other versions, use one of these documentation indexes:
> - 4.6 (Latest) (default): https://docs.nvidia.com/holoscan/sdk-user-guide/4-6-latest/llms.txt
> - ltsb-2.0: https://docs.nvidia.com/holoscan/sdk-user-guide/ltsb-2-0/llms.txt
> - 4.5: https://docs.nvidia.com/holoscan/sdk-user-guide/4-5/llms.txt
> - 4.4: https://docs.nvidia.com/holoscan/sdk-user-guide/4-4/llms.txt
> - 4.3: https://docs.nvidia.com/holoscan/sdk-user-guide/4-3/llms.txt
> - Archives: https://docs.nvidia.com/holoscan/sdk-user-guide/archives/llms.txt

> 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::InMemoryPubSubSession

> Process-global shared state for a group of in-memory pub/sub participants.

Process-global shared state for a group of in-memory pub/sub participants.

`InMemoryPubSubSession` consolidates the `SharedInMemoryPubSubBackend` and `SharedInMemoryPubSubSession` into a single, reusable class. It owns a shared `InMemoryDiscovery` and `InMemoryTransport`, and provides per-participant discovery/transport frontends that multiplex callbacks so that each [PubSubContext](pubsubcontext)'s `init_context()` wiring is independent. This was implemented to enable test cases that involve launching multiple GXF contexts from the same process.

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

**Inherits from:** `std::enable_shared_from_this< InMemoryPubSubSession >` (public)

---

## Constructors

### InMemoryPubSubSession \[#inmemorypubsubsession]

```cpp showLineNumbers={false}
holoscan::InMemoryPubSubSession::InMemoryPubSubSession(
    const std::string &session_id
)
```

### Destructor \[#destructor]

### \~InMemoryPubSubSession

```cpp showLineNumbers={false}
holoscan::InMemoryPubSubSession::~InMemoryPubSubSession()
```

---

## Methods

### create\_frontends \[#createfrontends]

```cpp showLineNumbers={false}
Frontends holoscan::InMemoryPubSubSession::create_frontends()
```

### join \[#join]

```cpp showLineNumbers={false}
void holoscan::InMemoryPubSubSession::join(
    gxf_context_t context
)
```

Join this session from a specific GXF context.

### leave \[#leave]

```cpp showLineNumbers={false}
void holoscan::InMemoryPubSubSession::leave(
    gxf_context_t context
)
```

Leave this session from a specific GXF context.

### is\_multi\_context \[#ismulticontext]

```cpp showLineNumbers={false}
bool holoscan::InMemoryPubSubSession::is_multi_context() const
```

Returns true if more than one distinct GXF context has joined.

### session\_id \[#sessionid]

```cpp showLineNumbers={false}
const std::string & holoscan::InMemoryPubSubSession::session_id() const
```

### ensure\_backends \[#ensurebackends]

```cpp showLineNumbers={false}
nvidia::gxf::Expected<void> holoscan::InMemoryPubSubSession::ensure_backends()
```

### retain\_participant \[#retainparticipant]

```cpp showLineNumbers={false}
nvidia::gxf::Expected<void> holoscan::InMemoryPubSubSession::retain_participant(
    const std::shared_ptr<InMemorySessionParticipant> &participant
)
```

### release\_participant \[#releaseparticipant]

```cpp showLineNumbers={false}
nvidia::gxf::Expected<void> holoscan::InMemoryPubSubSession::release_participant(
    const std::shared_ptr<InMemorySessionParticipant> &participant
)
```

### replay\_existing\_publishers \[#replayexistingpublishers]

```cpp showLineNumbers={false}
nvidia::gxf::Expected<void> holoscan::InMemoryPubSubSession::replay_existing_publishers(
    const std::shared_ptr<InMemorySessionParticipant> &participant
) const
```

### replay\_existing\_subscribers \[#replayexistingsubscribers]

```cpp showLineNumbers={false}
nvidia::gxf::Expected<void> holoscan::InMemoryPubSubSession::replay_existing_subscribers(
    const std::shared_ptr<InMemorySessionParticipant> &participant
) const
```

### announce\_publisher \[#announcepublisher]

```cpp showLineNumbers={false}
nvidia::gxf::Expected<void> holoscan::InMemoryPubSubSession::announce_publisher(
    const std::shared_ptr<InMemorySessionParticipant> &participant,
    const nvidia::gxf::PublisherInfo &info
)
```

### announce\_subscriber \[#announcesubscriber]

```cpp showLineNumbers={false}
nvidia::gxf::Expected<void> holoscan::InMemoryPubSubSession::announce_subscriber(
    const std::shared_ptr<InMemorySessionParticipant> &participant,
    const nvidia::gxf::SubscriberInfo &info
)
```

### remove\_publisher \[#removepublisher]

```cpp showLineNumbers={false}
nvidia::gxf::Expected<void> holoscan::InMemoryPubSubSession::remove_publisher(
    const std::shared_ptr<InMemorySessionParticipant> &participant,
    const nvidia::gxf::PublisherGid &gid
)
```

### remove\_subscriber \[#removesubscriber]

```cpp showLineNumbers={false}
nvidia::gxf::Expected<void> holoscan::InMemoryPubSubSession::remove_subscriber(
    const std::shared_ptr<InMemorySessionParticipant> &participant,
    const nvidia::gxf::SubscriberGid &gid
)
```

### query\_publishers \[#querypublishers]

```cpp showLineNumbers={false}
nvidia::gxf::Expected<std::vector<nvidia::gxf::PublisherInfo>> holoscan::InMemoryPubSubSession::query_publishers(
    const std::string &topic_name
) const
```

### query\_subscribers \[#querysubscribers]

```cpp showLineNumbers={false}
nvidia::gxf::Expected<std::vector<nvidia::gxf::SubscriberInfo>> holoscan::InMemoryPubSubSession::query_subscribers(
    const std::string &topic_name
) const
```

### get\_all\_topics \[#getalltopics]

```cpp showLineNumbers={false}
nvidia::gxf::Expected<std::vector<std::string>> holoscan::InMemoryPubSubSession::get_all_topics() const
```

### wait\_for\_subscribers \[#waitforsubscribers]

```cpp showLineNumbers={false}
bool holoscan::InMemoryPubSubSession::wait_for_subscribers(
    const std::string &topic_name,
    size_t expected_count,
    std::chrono::milliseconds timeout
) const
```

### send \[#send]

#### Const (1)

```cpp showLineNumbers={false}
nvidia::gxf::Expected<void> holoscan::InMemoryPubSubSession::send(
    const nvidia::gxf::Gid &destination_gid,
    const std::vector<uint8_t> &payload,
    const nvidia::gxf::MessageMetadata &metadata
) const
```

#### Const (2)

```cpp showLineNumbers={false}
nvidia::gxf::Expected<void> holoscan::InMemoryPubSubSession::send(
    const nvidia::gxf::Gid &destination_gid,
    std::vector<uint8_t> &&payload,
    const nvidia::gxf::MessageMetadata &metadata
) const
```

### send\_queue\_size \[#sendqueuesize]

```cpp showLineNumbers={false}
size_t holoscan::InMemoryPubSubSession::send_queue_size() const
```

### discovery \[#discovery]

```cpp showLineNumbers={false}
std::shared_ptr<nvidia::gxf::InMemoryDiscovery> holoscan::InMemoryPubSubSession::discovery() const
```

### transport \[#transport]

```cpp showLineNumbers={false}
std::shared_ptr<nvidia::gxf::InMemoryTransport> holoscan::InMemoryPubSubSession::transport() const
```

### snapshot\_participants \[#snapshotparticipants]

```cpp showLineNumbers={false}
std::vector<std::shared_ptr<InMemorySessionParticipant>> holoscan::InMemoryPubSubSession::snapshot_participants()
```

### cleanup\_expired\_participants\_locked \[#cleanupexpiredparticipantslocked]

```cpp showLineNumbers={false}
void holoscan::InMemoryPubSubSession::cleanup_expired_participants_locked()
```

### reset\_locked \[#resetlocked]

```cpp showLineNumbers={false}
void holoscan::InMemoryPubSubSession::reset_locked()
```

### notify\_publisher\_discovered \[#notifypublisherdiscovered]

```cpp showLineNumbers={false}
void holoscan::InMemoryPubSubSession::notify_publisher_discovered(
    const nvidia::gxf::PublisherInfo &info
)
```

### notify\_subscriber\_discovered \[#notifysubscriberdiscovered]

```cpp showLineNumbers={false}
void holoscan::InMemoryPubSubSession::notify_subscriber_discovered(
    const nvidia::gxf::SubscriberInfo &info
)
```

### notify\_publisher\_lost \[#notifypublisherlost]

```cpp showLineNumbers={false}
void holoscan::InMemoryPubSubSession::notify_publisher_lost(
    const nvidia::gxf::PublisherGid &gid
)
```

### notify\_subscriber\_lost \[#notifysubscriberlost]

```cpp showLineNumbers={false}
void holoscan::InMemoryPubSubSession::notify_subscriber_lost(
    const nvidia::gxf::SubscriberGid &gid
)
```

### make\_receive\_callback \[#makereceivecallback]

```cpp showLineNumbers={false}
static nvidia::gxf::PubSubTransport::ReceiveCallback holoscan::InMemoryPubSubSession::make_receive_callback(
    const std::shared_ptr<InMemorySessionParticipant> &participant
)
```

### registry \[#registry]

```cpp showLineNumbers={false}
static SessionRegistry & holoscan::InMemoryPubSubSession::registry()
```

---

## Static methods

### get\_or\_create \[#getorcreate]

```cpp showLineNumbers={false}
static std::shared_ptr<InMemoryPubSubSession> holoscan::InMemoryPubSubSession::get_or_create(
    const std::string &session_id
)
```

Get or create a session by ID.

Thread-safe. Returns the same shared\_ptr for the same session\_id within the process.

### reset\_all\_for\_testing \[#resetallfortesting]

```cpp showLineNumbers={false}
static void holoscan::InMemoryPubSubSession::reset_all_for_testing()
```

Release all sessions and reset shared state for tests.

Must only be called when no session frontends are active.

---

## Member variables

| Name                     | Type                                                                          | Description |
| ------------------------ | ----------------------------------------------------------------------------- | ----------- |
| `session_id_`            | `std::string`                                                                 |             |
| `mutex_`                 | `std::mutex`                                                                  |             |
| `subscriber_wait_mutex_` | `std::mutex`                                                                  |             |
| `subscriber_wait_cv_`    | `std::condition_variable`                                                     |             |
| `participants_`          | `std::unordered_map< uint64_t, std::weak_ptr< InMemorySessionParticipant > >` |             |
| `discovery_`             | `std::shared_ptr< nvidia::gxf::InMemoryDiscovery >`                           |             |
| `transport_`             | `std::shared_ptr< nvidia::gxf::InMemoryTransport >`                           |             |
| `next_participant_id_`   | `uint64_t`                                                                    |             |
| `context_mutex_`         | `std::mutex`                                                                  |             |
| `contexts_`              | `std::set< gxf_context_t >`                                                   |             |

---

## Inner classes

### Frontends

```cpp showLineNumbers={false}
struct holoscan::InMemoryPubSubSession::Frontends
```

Create a paired discovery + transport frontend that share the same participant state.

The discovery and transport frontends must be used together for a single [PubSubContext](pubsubcontext) — they share per-participant callback state so that PubSubContext::init\_context() wiring is independent per context.

| Name        | Type                                              | Description |
| ----------- | ------------------------------------------------- | ----------- |
| `discovery` | `std::shared_ptr< nvidia::gxf::PubSubDiscovery >` |             |
| `transport` | `std::shared_ptr< nvidia::gxf::PubSubTransport >` |             |