简体   繁体   English

如何从节点服务器通知 python 客户端?

[英]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/ .我有一个节点进程,它目前正在https://company/api/bats_hook/处接收 POST 请求。 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.每当有作业进入时,我想通知 python 进程。我使用的是 node_redis 和 redis-py,实现如下所示。

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?如何从 python 客户端订阅服务器上的端点? What is missing?什么不见了? I do have the redis server deployed on the server https://company/api/bats_hook/我确实在服务器https://company/api/bats_hook/上部署了 redis 服务器

javascript 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 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)

That's because you haven't configured Redis URL in python script.那是因为您没有在 python 脚本中配置 Redis URL 。 That's why it uses endpoint on localhost by default.这就是为什么它默认使用 localhost 上的端点。 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 indocumentation . 文档中的更多详细信息和示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM