drwurqczo39355510 2018-03-10 21:40
浏览 89

带REDIS的套接字IO。 服务器没有收到频道消息

I lost almost 2 days of googling and search trying to figure out what is wrong with my set up. All my services are running inside a Docker container, all properly connected (I have tested it)

I have a minimal Socket IO Server on Node.JS connected to Redis:

require('longjohn');
require('dotenv').load({silent: true});
process.env = process.env || {};

var PORT    = process.env.SOCKET_PORT || 4567;
var io      = require('socket.io').listen(PORT);
var redis   = require('socket.io-redis');

io.adapter(redis({
    host: process.env.REDIS_HOST || 'redis',
    port: process.env.REDIS_PORT || 6379
}));

io.on('connection', function (socket) {
    console.log(`New connection!`);
}

When I MONITOR this code on Redis, I got:

1520713149.833184 [0 172.20.0.8:40858] "info"
1520713149.833631 [0 172.20.0.8:40860] "info"
1520713149.834154 [0 172.20.0.8:40862] "info"
1520713149.839510 [0 172.20.0.8:40860] "subscribe" "socket.io#/#"
1520713149.840678 [0 172.20.0.8:40862] "subscribe" "socket.io-sync#request#/#"

Then a php App that connects and Emit to Redis

<?php

require(__DIR__.'/vendor/autoload.php');

$client = new Predis\Client([
    'scheme' => 'tcp',
    'host'   => 'redis',
    'port'   => 6379,
]);

$client->connect();
$emitter = new SocketIO\Emitter($client);
$emitter->emit('event', 'payload str');

And on Redis:

"PUBLISH" "socket.io#emitter" "\x92\x83\xa4type\x02\xa4data\x92\xa5event\xabpayload str\xa3nsp\xa1/\x82\xa5rooms\x90\xa5flags\x90"

So it looks like all come to REDIS but the socket io server doesn't get the event for an Unknown reason.

I was thinking about the different channel socket.io#emitter, socket.io#/# and socket.io-sync#request#/# used by both. But with all the article I have read, none of them used or changed them.

So maybe it's a package version that doesn't match? But all this version below are used by my co-workers... (I asked them for this problem, still looking)

On PHP, I use composer with:

{
    "require": {
        "rase/socket.io-emitter": "^0.7.0",
        "predis/predis": "^1.0"
    }
}

And on Socket IO Server, npm with:

{
  "dependencies": {
    "dotenv": "~1.2.0",
    "longjohn": "~0.2.9",
    "socket.io": "~1.3.7",
    "socket.io-redis": "^1.1",
  }
}

If someone has an idea, I'll appreciate a lot ;)

Edit: My co-workers use Vagrant and I gave it a try to see if something was different and yes, there is a difference in the channel subscribed by the socket.io server. It's psubscribe" "socket.io#* instead of socket.io#/# but they didn't touch anything about it and the sources are the same on both (docker and vagrant) kind of crazy right ?..

  • 写回答

1条回答 默认 最新

  • duangouhui0446 2018-03-12 09:46
    关注

    Ok so to separate the question and the solution. The problem here comes to my misunderstanding and lack of experience of socket IO and Redis.

    I was expected to have a new connection when a client Emit through Redis. In fact, that not the case, the new connection will only come from a client that connects to the socket of IO. I use the DEBUG=* to see everything happening on the server and indeed yes, socket IO get the information from REDIS. I was by mistake and lack of experience only focused on the channel name.

    I hope it will help a new padawan on Socket IO and Redis :)

    评论

报告相同问题?

悬赏问题

  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问