Listens to a Postgres channel and distributes any messages to HTTP clients via server-sent events (SSE).
The channel could be filled e.g. from a database trigger, as demonstrated in Brent Tubbs' Postgres + React TodoMVC Example. A new DB record would then trigger a notification to be sent to the channel, which is picked up by the pg_listen.rb
process, which posts it via HTTP to sse_server.rb
, which broadcasts it via SSE to all connected clients.
This is partially based on code in this gist.
$ bundle install
$ PORT=4567 DB_URL=postgres:///mydb SSE_URL=http://localhost:4567 foreman start
$ open http://localhost:4567/
Just listen in with curl
:
$ curl -s -H "Accept: text/event-stream" http://localhost:4567/stream
Just like the front end, I deploy this one to Bluemix with a simple cf push
. Have a look at the manifest.yml
for details. Heroku should be similar.
cf create-domain <org> <domain>.eu-gb.mybluemix.net
To keep things simple, it is best to choose the same domain as where the main web app was deployed to.
The URL to the SSE stream is required by both the listener and the front end. The SSE_URL
is set accordingly for both. Look at the manifest.yml
of the listener and the front end.
Please see CONTRIBUTING.md.
pg-sse
is Copyright© 2016 Steffen Uhlig. It is free software, and may be redistributed under the terms specified in the LICENSE file.