drogon
C++14/17-based HTTP application framework
Loading...
Searching...
No Matches
drogon::PubSubService< MessageType > Class Template Reference

This class template implements a publish-subscribe pattern with multiple named topics. More...

#include <drogon/PubSubService.h>

Inheritance diagram for drogon::PubSubService< MessageType >:
Collaboration diagram for drogon::PubSubService< MessageType >:

Public Types

using MessageHandler
using SharedMutex = std::shared_timed_mutex

Public Member Functions

void publish (const std::string &topicName, const MessageType &message) const
 Publish a message to a topic. The message will be broadcasted to every subscriber.
SubscriberID subscribe (const std::string &topicName, const MessageHandler &handler)
 Subscribe to a topic. When a message is published to the topic, the handler is invoked by passing the topic and message as parameters.
SubscriberID subscribe (const std::string &topicName, MessageHandler &&handler)
 Subscribe to a topic. When a message is published to the topic, the handler is invoked by passing the topic and message as parameters.
void unsubscribe (const std::string &topicName, SubscriberID id)
 Unsubscribe from a topic.
size_t size () const
 return the number of topics.
void clear ()
 remove all topics.
void removeTopic (const std::string &topicName)
 Remove a topic.
bool isTopicEmpty (const std::string &topicName) const
 Check if a topic is empty.

Detailed Description

template<typename MessageType>
class drogon::PubSubService< MessageType >

This class template implements a publish-subscribe pattern with multiple named topics.

Template Parameters
MessageTypeThe message type.

Member Typedef Documentation

◆ MessageHandler

template<typename MessageType>
using drogon::PubSubService< MessageType >::MessageHandler
Initial value:
std::function<void(const std::string &, const MessageType &)>

Member Function Documentation

◆ isTopicEmpty()

template<typename MessageType>
bool drogon::PubSubService< MessageType >::isTopicEmpty ( const std::string & topicName) const
inline

Check if a topic is empty.

Parameters
topicNameThe topic name.
Returns
true means there are no subscribers.
false means there are subscribers in the topic.

◆ subscribe()

template<typename MessageType>
SubscriberID drogon::PubSubService< MessageType >::subscribe ( const std::string & topicName,
MessageHandler && handler )
inline

Subscribe to a topic. When a message is published to the topic, the handler is invoked by passing the topic and message as parameters.

Parameters
topicNameTopic name.
handlerThe message handler.
Returns
The subscriber ID.

◆ unsubscribe()

template<typename MessageType>
void drogon::PubSubService< MessageType >::unsubscribe ( const std::string & topicName,
SubscriberID id )
inline

Unsubscribe from a topic.

Parameters
topicNameTopic name.
idThe subscriber ID returned from the subscribe method.

The documentation for this class was generated from the following file: