dty9731 2013-07-31 04:53
浏览 125

Redis Pub / Sub使用Python后端和Socket.io

I have a PHP code which publishes data to a channel called "MESSAGE_FROM_MARS". The snippet is as follows :

    function send_data_to_check_spam($feedback)
    {
            $d_id=$this->redis_connect(11);
            //echo $feedback;
            //die();
            echo "<b style='color:red'>MESSAGE SENT TO SPAM SWATTER</b>"."<br>";
            $d_id->PUBLISH("MESSAGE_FROM_MARS",$feedback);

    }

There is a server side python listener which receives the published data and the snippet is as follows :

r = redis.StrictRedis(host='localhost', port=6379, db=11)
def sum(a,b):
print a+b
def main():
  sub = r.pubsub()
  sub.subscribe('MESSAGE_FROM_MARS')

The python code does the processing and publishes the result back. The snippet is as follows :

r.publish('SPAM_STATUS',spam)

I am trying to get the result using a socket.io websocket and the snippet is as follows. :

<script>
var socket = io.connect('127.0.0.1:6379');
console.log(socket);
socket.on('SPAM_STATUS', function (data) {
alert("here");
console.log(data);
//socket.emit('my other event', { my: 'data' });
});
</script>

Everything works like a peach except that the socket.io for some reason is not getting the message from the SPAM_STATUS channel.

What am I doing wrong? I am relatively new to socket.io, so pardon my naivety

  • 写回答

1条回答 默认 最新

  • douhua1890 2013-12-11 01:30
    关注

    As far as I know, you need a socketIO server on the server side.

    So apparently, "var socket = io.connect('your redis port')" won't work.

    Redis only comes in for the pub/sub part, which is for your server->server broadcast.

    For realtime interaction between server and client side, you need a socketIO server AND socketIO client.

    this is working example that I find very useful myself:

    https://github.com/jonashagstedt/redis-pubsub-chat/tree/master/redischat

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程