Code Monkey home page Code Monkey logo

postgres-listener's Introduction

postgres-listener

Clojars Project Build Status

Connect to a postgres-database and wait for a NOTIFY of the database. This notify-event triggers a function, which receives the payload as EDN if the payload was initially encoded as JSON (for example with postgres' function row_to_json().

A trigger for a table 'events' can be configured like this:

-- language: PLpgSQL
-- Define function for notify-event
CREATE OR REPLACE FUNCTION PUBLIC.NOTIFY() RETURNS trigger AS
$BODY$
BEGIN
PERFORM pg_notify('new_event', row_to_json(NEW)::text);
RETURN new;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE COST 100;)

-- Creates trigger and binds it to \"events\"-table
CREATE TRIGGER new_event
AFTER INSERT
ON events
FOR EACH ROW
EXECUTE PROCEDURE PUBLIC.NOTIFY();

Usage

Require latest version of postgres-listener in your project.clj (check latest version in the badge as this string is very likely to be forgotten on new releases):

:dependencies [[de.hhu.cn/postgres-listener "0.1.2"]]

You can then use it in your code to connect to the database, which provides the NOTIFY channel and add a listener to the corresponding LISTEN event. If your table is configured as shown above, you can react to the trigger new_event like this:

(require '[postgres-listener.core :as pgl])

(pgl/connect {:host "localhost" :port 5432 :database "postgres" :user "postgres" :password "postgres"})
(pgl/arm-listener (fn [payload] (println payload)) "new_event")

License

Copyright © 2017 Christian Meter

Distributed under the MIT license.

postgres-listener's People

Contributors

n2o avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.