slack-client icon indicating copy to clipboard operation
slack-client copied to clipboard

Websocket produces duplicate messages

Open ElvenSpellmaker opened this issue 7 years ago • 0 comments

Hi there, I believe this is a bug with the websocket package and not your package directly, but every event comes in twice (with the same payload) which causes everything to be triggered twice:

object(Devristo\Phpws\Messaging\WebSocketMessage)#53 (2) {
  ["frames":protected]=>
  array(1) {
    [0]=>
    object(Devristo\Phpws\Framing\WebSocketFrame)#2094 (10) {
      ["FIN":protected]=>
      int(1)
      ["RSV1":protected]=>
      int(0)
      ["RSV2":protected]=>
      int(0)
      ["RSV3":protected]=>
      int(0)
      ["opcode":protected]=>
      int(1)
      ["mask":protected]=>
      int(0)
      ["payloadLength":protected]=>
      int(63)
      ["maskingKey":protected]=>
      int(0)
      ["payloadData":protected]=>
      string(63) "{"type":"user_typing","channel":"D214XLEJG","user":"U096BVDUM"}"
      ["actualLength":protected]=>
      int(0)
    }
  }
  ["data":protected]=>
  string(0) ""
}
object(Devristo\Phpws\Messaging\WebSocketMessage)#53 (2) {
  ["frames":protected]=>
  array(1) {
    [0]=>
    object(Devristo\Phpws\Framing\WebSocketFrame)#2094 (10) {
      ["FIN":protected]=>
      int(1)
      ["RSV1":protected]=>
      int(0)
      ["RSV2":protected]=>
      int(0)
      ["RSV3":protected]=>
      int(0)
      ["opcode":protected]=>
      int(1)
      ["mask":protected]=>
      int(0)
      ["payloadLength":protected]=>
      int(63)
      ["maskingKey":protected]=>
      int(0)
      ["payloadData":protected]=>
      string(63) "{"type":"user_typing","channel":"D214XLEJG","user":"U096BVDUM"}"
      ["actualLength":protected]=>
      int(0)
    }
  }
  ["data":protected]=>
  string(0) ""
}
object(Devristo\Phpws\Messaging\WebSocketMessage)#53 (2) {
  ["frames":protected]=>
  array(1) {
    [0]=>
    object(Devristo\Phpws\Framing\WebSocketFrame)#2094 (10) {
      ["FIN":protected]=>
      int(1)
      ["RSV1":protected]=>
      int(0)
      ["RSV2":protected]=>
      int(0)
      ["RSV3":protected]=>
      int(0)
      ["opcode":protected]=>
      int(1)
      ["mask":protected]=>
      int(0)
      ["payloadLength":protected]=>
      int(116)
      ["maskingKey":protected]=>
      int(0)
      ["payloadData":protected]=>
      string(116) "{"type":"message","channel":"D214XLEJG","user":"U096BVDUM","text":"Hey","ts":"1488026184.000017","team":"T02TZ7DQW"}"
      ["actualLength":protected]=>
      int(0)
    }
  }
  ["data":protected]=>
  string(0) ""
}
object(Devristo\Phpws\Messaging\WebSocketMessage)#53 (2) {
  ["frames":protected]=>
  array(1) {
    [0]=>
    object(Devristo\Phpws\Framing\WebSocketFrame)#2094 (10) {
      ["FIN":protected]=>
      int(1)
      ["RSV1":protected]=>
      int(0)
      ["RSV2":protected]=>
      int(0)
      ["RSV3":protected]=>
      int(0)
      ["opcode":protected]=>
      int(1)
      ["mask":protected]=>
      int(0)
      ["payloadLength":protected]=>
      int(116)
      ["maskingKey":protected]=>
      int(0)
      ["payloadData":protected]=>
      string(116) "{"type":"message","channel":"D214XLEJG","user":"U096BVDUM","text":"Hey","ts":"1488026184.000017","team":"T02TZ7DQW"}"
      ["actualLength":protected]=>
      int(0)
    }
  }
  ["data":protected]=>
  string(0) ""
}

The only way I've been able to mitigate this is to do something like:

$messages = [];

$client->on('message', function ($data) use (&$messages) {
    $dataArray = $data->jsonSerialize();

    if ($key = array_search($dataArray, $messages, true))
    {
        unset($key);
        return;
    }
    $messages[] = $data;

ElvenSpellmaker avatar Feb 25 '17 13:02 ElvenSpellmaker