Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. PostgreSQL event notification with payload
Forum Update on Monday, May 27th 2025

PostgreSQL event notification with payload

Scheduled Pinned Locked Moved Solved 3rd Party Software
6 Posts 2 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    Ucn_
    wrote on 10 Dec 2019, 18:36 last edited by
    #1

    Hello. I was using ODBC driver for sql server, however after learning that ODBC doesn't support eventNotification I tried to add that feature by building the driver with no success I changed to PSQL driver and used psql by downloading the binary files and adding to the path. It worked. I test the event notification and it works. However I would like to send the notification with the affected row information. I was able to connect with the two signal and slot, but I don't know how to work with the payload. I am new to postgres. This is how I achieved the event notification.

    In the postgresql server:

    CREATE RULE Eventnotifications AS ON INSERT TO test DO NOTIFY sendToEventNots
    

    In my app:

    bool ok = db.open();
    
            if (ok)
            {
                qDebug() << "Database connected";
                bool eventNot= db.driver()->hasFeature(QSqlDriver::EventNotifications);
                qDebug() << "Has feature eventNotification" << eventNot;
                db.driver()->subscribeToNotification("sendToEventNots");
                qDebug() << "Connected Info" <<  db.databaseName();
     
            }
    
            connect(db.driver(), SIGNAL(notification(const QString&)),
                    this, SLOT(notificationReceived(const QString&)));
    
            connect(db.driver(), QOverload<const QString &>::of(&QSqlDriver::notification),
                [this](const QString &sendToEventNots){
    
                qDebug() << "Event received";
            });
    }
    
    void MainWindow::notificationReceived(const QString&){
    
       qDebug() << "Notification received";
    }
    
    

    The above two connect work. I would like to do something like this:
    In server:

    CREATE RULE Eventnotifications AS ON INSERT TO test DO NOTIFY sendToEventNots, {"userid":the user Id, "value1": value1,"value2":value2,"action":"grok"}
    

    in my app:

    connect(sqlDriver, QOverload<const QString &, QSqlDriver::NotificationSource, const QVariant &>::of(&QSqlDriver::notification),
        [=](const QString &name, QSqlDriver::NotificationSource source, const QVariant &payload){ /* ... */ });
    

    How can I achieve this, and be able to receive the values? Is it possible? Thanks

    1 Reply Last reply
    0
    • U Offline
      U Offline
      Ucn_
      wrote on 10 Dec 2019, 22:56 last edited by
      #6

      I solved by creating a stored procedure then passing the values to a json object. Created a trigger on Insert then execute the procedure. In my app it detects the source with switch case. I followed some examples and implemented to my needs.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 10 Dec 2019, 20:54 last edited by
        #2

        Hi,

        Don't you have them in the QVariant you receive ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        U 1 Reply Last reply 10 Dec 2019, 20:57
        1
        • S SGaist
          10 Dec 2019, 20:54

          Hi,

          Don't you have them in the QVariant you receive ?

          U Offline
          U Offline
          Ucn_
          wrote on 10 Dec 2019, 20:57 last edited by
          #3

          @SGaist I don't know what the source is.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 10 Dec 2019, 20:59 last edited by
            #4

            What source are you talking about ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            U 1 Reply Last reply 10 Dec 2019, 21:10
            1
            • S SGaist
              10 Dec 2019, 20:59

              What source are you talking about ?

              U Offline
              U Offline
              Ucn_
              wrote on 10 Dec 2019, 21:10 last edited by
              #5

              @SGaist In the last example, I believe I'm not setting the payload the right way on the server. Maybe the way I do, I receive empty Qstring

              1 Reply Last reply
              0
              • U Offline
                U Offline
                Ucn_
                wrote on 10 Dec 2019, 22:56 last edited by
                #6

                I solved by creating a stored procedure then passing the values to a json object. Created a trigger on Insert then execute the procedure. In my app it detects the source with switch case. I followed some examples and implemented to my needs.

                1 Reply Last reply
                0

                1/6

                10 Dec 2019, 18:36

                • Login

                • Login or register to search.
                1 out of 6
                • First post
                  1/6
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved