Wt  4.14.1
Loading...
Searching...
No Matches
Public Member Functions | List of all members
Wt::Dbo::NotificationListener Class Reference

Class for sending and listening for notification trough a database. More...

#include <Wt/Dbo/NotificationListener.h>

Public Member Functions

 NotificationListener ()
 Create a NotificationListener.
 
 NotificationListener (std::unique_ptr< SqlConnection > connection)
 Create and connect a NotificationListener.
 
 ~NotificationListener ()
 Destroy the NotificationListener.
 
void connect (std::unique_ptr< SqlConnection > connection)
 Connect the NotificationListener to a database.
 
void disconnect ()
 Disconnect the NotificationListener.
 
void notify (const std::string &channel, const std::string &message="")
 Send a notification.
 
template<class C >
void notify (const ptr< C > &obj)
 Send a notification.
 
void setCallback (const std::string &channel, const std::function< void(const std::string &, const std::string &)> &callback)
 Sets a function to be called when a notification is received.
 
void removeCallback (const std::string &channel)
 Removes the callback for the given channel.
 

Detailed Description

Class for sending and listening for notification trough a database.

A NotificationListener send notification trough a database and listen for notification from the database it is connected to. Each notification is send trough a 'channel' and for each notification recieved on a channel, the NotificationListener will emit a signal which is unique for each channel.

Note
This class needs server-initiated updates to be enabled in order to work. This will automatically be activated when connecting to a database.
Warning
The notifications are currently only implemented for postgresql databases. In order to use notifications on other type of databases, a custom implementation of the functions subscribe , notify , getNextNotify , setupNotify and stopListen must be implemented for the specific database. This can be done by inheriting from the connection to the specific database and overriding those functions.

Constructor & Destructor Documentation

◆ NotificationListener()

Wt::Dbo::NotificationListener::NotificationListener ( std::unique_ptr< SqlConnection connection)
explicit

Create and connect a NotificationListener.

Create a new NotificationListener and directly connect it to the database usig the given connection.

Member Function Documentation

◆ connect()

void Wt::Dbo::NotificationListener::connect ( std::unique_ptr< SqlConnection connection)

Connect the NotificationListener to a database.

Connect the NotificationListener to the database using the given connection. The connection will be used exclusively by this NotificationListener.

Calling this function will destroy any connection already used by the NotificationListener.

Note
This enables server-initiated updates.
See also
disconnect()

◆ disconnect()

void Wt::Dbo::NotificationListener::disconnect ( )

Disconnect the NotificationListener.

Disconnect the NotificationListener from the database it is connected to.

Note
The signals for some notifications that arrived before the call of disconnect might still be emited after that call.

◆ notify() [1/2]

template<class C >
void Wt::Dbo::NotificationListener::notify ( const ptr< C > &  obj)

Send a notification.

Helper function that sends a notification on the channel with the name of the table the object of the pointer belogns to, and with the id of the object as the message.

See also
setCallback()

◆ notify() [2/2]

void Wt::Dbo::NotificationListener::notify ( const std::string &  channel,
const std::string &  message = "" 
)

Send a notification.

Sends a notification with the given message on the given channel. If no message is given, en empty string will be sent as message.

See also
setCallback()

◆ removeCallback()

void Wt::Dbo::NotificationListener::removeCallback ( const std::string &  channel)

Removes the callback for the given channel.

This removes the callback function for the given channel.

Warning
Since the callback functions are called from another thread, the deleted function might still be called after this function returns.
See also
setCallback()

◆ setCallback()

void Wt::Dbo::NotificationListener::setCallback ( const std::string &  channel,
const std::function< void(const std::string &, const std::string &)> &  callback 
)

Sets a function to be called when a notification is received.

This sets the function callback as the function to be called when a notification is received on the given channel. The callback function will take two strings as arguments. The first string is the message received and the second string is the name of the channel the notification was sent on.

Warning
Since the callback functions are called from another thread, the old function might still be called after this function returns.
See also
removeCallback(), notify()