Skip to content

Instantly share code, notes, and snippets.

@quommit
Created March 5, 2015 13:16
Show Gist options
  • Save quommit/5b7b469648c795534018 to your computer and use it in GitHub Desktop.
Save quommit/5b7b469648c795534018 to your computer and use it in GitHub Desktop.
Simple PostgreSQL plpython function for message publication through Redis pub/sub
CREATE OR REPLACE FUNCTION redis_pubsub.notify(channel text, message text, host text, port integer)
RETURNS void AS
$BODY$
#Safeguard against null or empty values
if (channel is None) or (message is None):
return
if (host is None) or (host.strip() == ""):
redishost = "127.0.0.1"
else:
redishost = host
if (port is None) or (port <= 0):
redisport = 6379
else:
redisport = port
import redis
#Get Redis connection
r = redis.StrictRedis(host=redishost, port=redisport, db=0)
#Publish message
r.publish(channel, message)
$BODY$
LANGUAGE plpython3u VOLATILE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment