Skip to content

Commit f5f39b4

Browse files
Update notifications.py
1 parent 332b5f8 commit f5f39b4

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

notifications.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,24 @@ def handle_connect():
55
@socketio.on('disconnect')
66
def handle_disconnect():
77
print('Client disconnected')
8+
9+
@socketio.on('subscribe')
10+
def handle_subscribe():
11+
conn.cursor().execute('LISTEN user_changed')
12+
13+
14+
CREATE OR REPLACE FUNCTION notify_user_changed()
15+
RETURNS trigger AS
16+
$$
17+
BEGIN
18+
PERFORM pg_notify('user_changed', NEW.id::text);
19+
RETURN NEW;
20+
END;
21+
$$ LANGUAGE plpgsql;
22+
23+
CREATE TRIGGER user_changed_trigger
24+
AFTER INSERT OR UPDATE OR DELETE ON users
25+
FOR EACH ROW
26+
EXECUTE PROCEDURE notify_user_changed();
27+
28+

0 commit comments

Comments
 (0)