{"id":14064716,"url":"https://github.com/frafra/postgresql2websocket","last_synced_at":"2025-10-15T05:20:38.251Z","repository":{"id":16370265,"uuid":"79792027","full_name":"frafra/postgresql2websocket","owner":"frafra","description":"Send PostgreSQL notifications over websockets","archived":false,"fork":false,"pushed_at":"2023-03-13T04:59:31.000Z","size":75,"stargazers_count":81,"open_issues_count":5,"forks_count":10,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-02-27T15:38:07.793Z","etag":null,"topics":["aiohttp","asyncpg","notifications","postgresql","python","python3","python35","python36","python37","trigger","websockets"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/frafra.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2017-01-23T09:58:29.000Z","updated_at":"2024-12-24T15:05:32.000Z","dependencies_parsed_at":"2024-06-11T17:09:59.463Z","dependency_job_id":null,"html_url":"https://github.com/frafra/postgresql2websocket","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frafra%2Fpostgresql2websocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frafra%2Fpostgresql2websocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frafra%2Fpostgresql2websocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frafra%2Fpostgresql2websocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/frafra","download_url":"https://codeload.github.com/frafra/postgresql2websocket/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243835942,"owners_count":20355611,"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":["aiohttp","asyncpg","notifications","postgresql","python","python3","python35","python36","python37","trigger","websockets"],"created_at":"2024-08-13T07:04:02.172Z","updated_at":"2025-10-15T05:20:33.223Z","avatar_url":"https://github.com/frafra.png","language":"Python","readme":"# postgresql2websocket\n\n[![CircleCI](https://img.shields.io/circleci/build/github/frafra/postgresql2websocket.svg)](https://circleci.com/gh/frafra/workflows/postgresql2websocket)\n\n## How does it work?\n\nExecute `postgresql2websocket.py` and connect to `ws://localhost:8080/channel` where `channel` is the channel name of the selected database (see [PostgreSQL documentation - NOTIFY](https://www.postgresql.org/docs/current/static/sql-notify.html)).\n\n## How to configure it?\n\nCopy `postgresql2websocket.conf.example` as `postgresql2websocket.conf` and change the values according to your needs.\n\n## What technologies are you using?\n\n* [PostgreSQL](https://www.postgresql.org/)\n* [Python](https://www.python.org/) (3.5.3 or greater)\n* [aiohttp](http://aiohttp.readthedocs.io/) + [asyncpg](https://github.com/MagicStack/asyncpg)\n* [Poetry](https://poetry.eustace.io/) for dependency management\n\n## What can I do with it?\n\nYou can create a trigger on a table in order to get notifications over a websocket in real time. For example, this function send a notification using the `postgresql2websocket` channel containing the action that has been performed and a JSON representation of the row.\n```\nCREATE OR REPLACE FUNCTION table_update_notify() RETURNS trigger AS $$\nDECLARE\n  rec RECORD;\nBEGIN\n  IF TG_OP = 'INSERT' OR TG_OP = 'UPDATE' THEN\n    rec = NEW;\n  ELSE\n    rec = OLD;\n  END IF;\n  -- postgresql2websocket is the default channel name\n  PERFORM pg_notify('postgresql2websocket', json_build_object('table', TG_TABLE_NAME, 'type', TG_OP, 'row', row_to_json(rec))::text);\n  RETURN NEW;\nEND;\n$$ LANGUAGE plpgsql;\n```\n\nThen you can trigger this function when something happens on `mytable`:\n```\nCREATE TRIGGER mytable_notify_update AFTER UPDATE ON mytable FOR EACH ROW EXECUTE PROCEDURE table_update_notify();\nCREATE TRIGGER mytable_notify_insert AFTER INSERT ON mytable FOR EACH ROW EXECUTE PROCEDURE table_update_notify();\nCREATE TRIGGER mytable_notify_delete AFTER DELETE ON mytable FOR EACH ROW EXECUTE PROCEDURE table_update_notify();\n```\n\n## Bidirectional communication\n\nThe websocket channel can be used to execute remote functions too:\n\nFor example:\n```\nCREATE FUNCTION add(integer, integer) RETURNS integer\n    AS 'select $1 + $2;'\n    LANGUAGE SQL\n    IMMUTABLE\n    RETURNS NULL ON NULL INPUT;\n```\n\nExecuting `ws.send('select add(1, 2);')` will return a JSON response containing `{\"add\": 3}`.\n\n## How to use it?\n\nExecute `postgresql2websocket.py`, open `examples/console.html` and execute some operations on the table.\n\n## Limitations\n\n\u003e The \"payload\" string to be communicated along with the notification. This must be specified as a simple string literal. In the default configuration it must be shorter than 8000 bytes. (If binary data or large amounts of information need to be communicated, it's best to put it in a database table and send the key of the record.)\n\nSource: https://www.postgresql.org/docs/current/static/sql-notify.html\n","funding_links":[],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrafra%2Fpostgresql2websocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffrafra%2Fpostgresql2websocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrafra%2Fpostgresql2websocket/lists"}