I'm writing C++ applicatoin which needs to receive notifications for data changes from PostgreSQL through libpqxx library. But it's tutorial doesn't include such use case. The notifications must be received on multiple channels. Also I'm using boost::asio as networking library and for me is preferable if possible to use asio socket classes with asynchronous callbacks for notification events instead of polling of raw BSD style sockets. Can someone provide sample code for this or links to some external resources for how this can be achieved?
Asked
Active
Viewed 2,170 times
3
-
Who told you that you can have Postgresql notify a client for "data changes"? There's nothing of that kind in Postgresql. The only thing that Postgresql has is an abstract listen/notify signaling facility between cooperating client processes. – Sam Varshavchik Jul 19 '16 at 12:43
-
2I mean NOTIFY functionality which can be used alongside triggers. [Here](https://www.postgresql.org/docs/9.5/static/sql-notify.html) is the documentation. The client application can open socket to the database and listen for notifications on given channel. – bobeff Jul 19 '16 at 13:16
1 Answers
3
You need a class derived from pqxx::notification_receiver
, see http://pqxx.org/devprojects/libpqxx/doc/4.0/html/Reference/a00208.html "Notifications and Receivers" and
http://pqxx.org/devprojects/libpqxx/doc/4.0/html/Reference/a00062.html which is the API reference for notification_receiver
.

NovaDenizen
- 5,089
- 14
- 28
-
Is it possible to integrate notifications receiving with _boost::asio_ event loop? – bobeff Jul 25 '16 at 13:27
-
If avoiding polling is really that important, I'd use libpq instead of libpqxx. See https://www.postgresql.org/docs/9.1/static/libpq-notify.html for advice on avoiding polling. Less confident: I think direct integration with asio is essentially impossible. I'd run a separate thread exclusively to receive the notifications with libpq, then pass the notifications over to the asio hairball with io_service::post. – NovaDenizen Jul 26 '16 at 03:17
-
10x I managed to integrate *libpqxx* with *boost::asio* using *null_buffers* functionality of *asio*. – bobeff Jul 26 '16 at 07:27
-