Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to notify a python client from node server?

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/

javascript

    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
      }); 
    });

python

    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)
like image 566
Ritz Avatar asked Nov 18 '25 03:11

Ritz


1 Answers

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.

like image 187
wowkin2 Avatar answered Nov 19 '25 16:11

wowkin2