walkor / phpsocket.io

A server side alternative implementation of socket.io in PHP based on workerman.
2.3k stars 508 forks source link

Channel issue due to package_length. Another reason #303

Open quietasice opened 6 months ago

quietasice commented 6 months ago

Hi @walkor. First of all thank you so much for your socket.io port.

I'm getting a lot of errors Error package. package_length= and started to investigate it. I found your posts about the client sending the wrong packet. This could be the reason, but I found another case. Im getting errors when worker's clients cross 7-10k connections.

To be able to send a message to a specific connection, you defined each "sid" as a room. https://github.com/walkor/phpsocket.io/blob/master/src/Socket.php#L246

public function join($room)
{
    if (! $this->connected) {
        return $this;
    }
    if (isset($this->rooms[$room])) {
        return $this;
    }
    $this->adapter->add($this->id, $room);
    $this->rooms[$room] = $room;
    return $this;
    }

and the adapter will subscribe to the "sid\room" https://github.com/walkor/phpsocket.io/blob/master/src/ChannelAdapter.php#L33

public function add($id, $room)
{
    $this->sids[$id][$room] = true;
    $this->rooms[$room][$id] = true;
    $channel = "socket.io#/#$room#";
    \Channel\Client::subscribe($channel);
}

Each time a client establishes a connection, a message to the channel includes all worker's SIDs https://github.com/walkor/channel/blob/master/src/Client.php#L199

public static function onRemoteConnect()
{
    $all_event_names = array_keys(self::$_events);
    if($all_event_names)
    {
        self::subscribe($all_event_names);
    }
    self::clearTimer();

    if (self::$onConnect) {
        call_user_func(Client::$onConnect);
    }
}

https://github.com/walkor/channel/blob/master/src/Client.php#L250

public static function subscribe($events)
{
    $events = (array)$events;
    self::send(array('type' => 'subscribe', 'channels'=>$events));
    foreach ($events as $event) {
        if(!isset(self::$_events[$event])) {
            self::$_events[$event] = null;
        }
    }
}

similar to this a:2:{s:4:"type";s:9:"subscribe";s:8:"channels";a:4:{i:0;s:12:"socket.io#/#";i:1;s:37:"socket.io#/#c66cbafbbf80d94105ab5045#";i:2;s:37:"socket.io#/#44dfbbfbbf80d941048015df#";i:3

which will definitely start hitting the packet size limit sooner or later.

Is it make sense for you? just want let people know, who will get same errors that the reason may not be due to wrong packet, but to a simpler reason of growth

This problem can be solved by adding more nodes to reduce the average number of connections per node, but this is likely a workaround. I think I'll look into Redis as a channel adapter.