场景:发布到名为“message”的 redis channel 的 PHP 应用位于 IP 1.2.3.4
如何从 Node 连接到在该 IP 上的 channel ?
var listener = redis.createClient();
listener.subscribe('message', /* ? '1.2.3.4' ? */);
listener.on("messages", function(channel, message) {
// do something with data
});
最佳答案
我假设您使用的是 node_redis .您可以指定您正在使用的主机:
redis.createClient(port, host, options)
---
Create a new client connection.
port defaults to 6379 and host defaults to 127.0.0.1.
所以你应该使用:
redis.createClient('1.2.3.4');
它在文档中非常好,我认为您应该完整地阅读它。
P.S:当您使 redis 可用于远程主机时,我认为您应该正确设置防火墙以拒绝来自其他 IP(白名单)的访问。
关于Node.JS 订阅给定 IP 上的 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6417237/