Posted 4 years ago.
It takes merely 5 minutes to write an observer using Pomm2:
The PHP code is the following:
It is a very simple piece of code, it does wait for a postgesql's
NOTIFY command on the
pika channel. When a notification is received, it is printed on the console. If the data attached with the event is
stop, the script exits.
Let's have a look in Postgresql's logs to see what happened:
10:46:32 UTC LOG: duration: 0.144 ms statement: set "bytea_output" = 'hex'; set "intervalstyle" = 'ISO_8601'; set "datestyle" = 'ISO' 10:46:32 UTC LOG: duration: 0.031 ms statement: listen "pika" 10:46:40 UTC LOG: duration: 0.530 ms statement: notify pika, 'pikaaaa'; 10:46:46 UTC LOG: duration: 0.152 ms statement: notify pika, 'chu'; 10:46:50 UTC LOG: duration: 0.179 ms statement: notify pika, 'stop'; 10:46:50 UTC LOG: duration: 0.188 ms statement: unlisten "pika"
The first line is the connection configuration at startup. This connection is opened because of the first call to
getObserver('pika'). Since the observer did not exist before, it is created and a
listen 'pika' is issued. The script now silently asks the server if new notification arrived every second. 8 seconds later, a notification is emitted from a psql command line. The log shows only one connection is opened to the server and is kept during the loop with the
$session variable. Once the notification with the
stop payload is sent, the PHP script exits and successfully
unlisten the observer before closing the connection.
Of course, this observer is useless, it can be used as a base for a more useful one.