{"id":15725904,"url":"https://github.com/ndrean/async_pg","last_synced_at":"2026-05-15T08:40:17.924Z","repository":{"id":124424090,"uuid":"476827131","full_name":"ndrean/async_pg","owner":"ndrean","description":"pg_notify in Ruby with `Sequel.listen` and websockets","archived":false,"fork":false,"pushed_at":"2022-04-01T18:21:30.000Z","size":3,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-31T01:28:45.987Z","etag":null,"topics":["faye-websocket","notifications","notify","postgres","ruby","sequel","websocket"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ndrean.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-04-01T18:21:20.000Z","updated_at":"2022-04-01T18:45:26.000Z","dependencies_parsed_at":null,"dependency_job_id":"3f4fa2e0-1a57-4305-b020-e8e5c7497739","html_url":"https://github.com/ndrean/async_pg","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ndrean/async_pg","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ndrean%2Fasync_pg","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ndrean%2Fasync_pg/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ndrean%2Fasync_pg/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ndrean%2Fasync_pg/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ndrean","download_url":"https://codeload.github.com/ndrean/async_pg/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ndrean%2Fasync_pg/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264871482,"owners_count":23676453,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["faye-websocket","notifications","notify","postgres","ruby","sequel","websocket"],"created_at":"2024-10-03T22:24:56.807Z","updated_at":"2025-10-08T15:30:15.697Z","avatar_url":"https://github.com/ndrean.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Source\n\n\u003chttps://citizen428.net/blog/asynchronous-notifications-in-postgres/\u003e\n\n\u003chttps://www.enterprisedb.com/postgres-tutorials/everything-you-need-know-about-postgresql-triggers\u003e\n\n\u003e Note: \u003chttps://github.com/bensheldon/rails_postgres_websockets_chat/blob/master/app/lib/async_events.rb\u003e\n\u003e Potential gem for ActiveRecord: \u003chttps://github.com/ezcater/activerecord-postgres_pub_sub\u003e\n\n- run `$ psql -U postgres` to connect to postgres\n\n- create database and connect ot database\n\n```bash\npostgres=# CREATE DATABASE notify-_test;\npostgres=# \\c notify_test;\n```\n\n- create table (in psql)\n\n```bash\npostgres=# CREATE TABLE orders (...\n```\n\n- create the function and trigger event so Postgres will asynchronously send a message.\n\n```bash\n# Trigger function creation\npostgres=# CREATE OR REPLACE FUNCTION notify_event() RETURNS TRIGGER...\n\n# sets the trigger function in 'orders' table\npostgres=# CREATE TRIGGER notify_order_event...\n\n# check the trigger on the table 'orders'\npostgres=# \\dS orders\n...\nTriggers:\n    notify_order_event AFTER INSERT OR DELETE OR UPDATE ON orders FOR EACH ROW EXECUTE FUNCTION notify_event()\n\n```\n\n\u003e Note : to remove a trigger, do:\n\n```bash\npostgres=# drop trigger notify_order_event on \"orders\" ;\n```\n\n- create a lambda in `test2.rb` (so it responds to `lambada.call`) that:\n\n  - uses `Sequel.listen` on the Postgres event,\n\n  - creates a websocket with the `faye`gem to broadcast the payload\n\n- create the `index.html` that contains a script to launch the native `WebSocket`.\n\n- create `config.ru` that contains `run App` (so rackup can `call`)\n\n\nThen run in a terminal (it will start the Puma webserver by default):\n\n```bash\nrackup --env production config.ru\n```\n\nAdd a new row to the database by running in a psql session:\n\n```bash\npostgres=# INSERT into orders (email, total) VALUES ('test10@ex.com', 10);\n```\n\nAnd observe the result in several browsers (with logs)\n\n## Run this in migration\n\n\u003chttps://stackoverflow.com/questions/39755504/how-do-i-create-a-trigger-in-a-rails-migration\u003e\n\n\u003chttps://sequel.jeremyevans.net/rdoc/classes/Sequel/Migrator.html\u003e\n\n\n```bash\nirb\u003e require 'sequel'\nirb\u003e Sequel.extension :migration, :core_extensions\n\nirb\u003e Sequel.connect('postgres://postgres@localhost/notify_test') do |db\n| Sequel::Migrator.run(db, '.')\n```\n\n```rb\nclass CreateTriggers \u003c ActiveRecord::Migration\n  def change\n    execute \u003c\u003c-SQL\n\n      CREATE TABLE IF NOT EXISTS orders (\n         id SERIAL PRIMARY KEY,\n         email TEXT NOT NULL,\n         total BIGINT NOT NULL\n      );\n\n\n      CREATE OR REPLACE FUNCTION notify_event()\n         RETURNS trigger AS $$\n         DECLARE ...\n         BEGIN\n            ...\n            PERFORM pg_notify('events', payload::text);\n            RETURN NULL;\n         END\n        LANGUAGE plpgsql AS\n        $$\n        BEGIN\n        ...\n      END;$$;\n\n      DROP TRIGGER IF EXISTS notify_order_event\n         on orders;\n\n      CREATE TRIGGER notify_order_event...\n\n      CREATE TRIGGER yyy\n    SQL\n  end\nend\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fndrean%2Fasync_pg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fndrean%2Fasync_pg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fndrean%2Fasync_pg/lists"}