I have a node process which is currently receiving POST requests at https://company/api/bats_hook/. I want to notify a python process whenever a job comes in. I am using node_redis and redis-py, the implementation looks like below.
Following code works great locally when the node process is running locally, when I push the node endpoint to a server, I don't seem to get the events?
How do I subscribe to the endpoint on the server from python client? What is missing? I do have the redis server deployed on the server https://company/api/bats_hook/
var redis = require("redis"),
//redisClient = redis.createClient();
redisClient = redis.createClient({url: process.env.REDIS_URL});
app.post("/api/bats_holder", (req, res, next) => {
console.log(req.query.params)
console.log(req.body)
console.log(req.body.name)
// This publishes a message to the "bats hook channel"
redisClient.publish("bats hook channel", JSON.stringify({
params: req.query.params,
body: req.body,
}));
res.status(200).json({
message: "BATS object",
posts: req.body
});
});
import redis
r = redis.Redis()
p = r.pubsub()
p.subscribe('bats hook channel')
# This blocks and reads any messages from the "bats hook channel" as they come in
for message in p.listen():
print(message)
That's because you haven't configured Redis URL in python script. That's why it uses endpoint on localhost by default. Update your script with following.
import os
import redis
REDIS_URL = os.environ.get('REDIS_URL', 'redis://localhost:6379/0')
r = redis.from_url(REDIS_URL)
p = r.pubsub()
p.subscribe('bats hook channel')
# This blocks and reads any messages from the "bats hook channel" as they come in
for message in p.listen():
print(message)
More details and examples in documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With