Skip to content

Possible Issue with Binary Message Recognition #436

@med-galaxies

Description

@med-galaxies

Description

When emitting binary data (e.g., Node.js Buffer or Flutter Uint8List) via Socket.IO, the C++ client (socket.io-client-cpp) fails to correctly identify binary payloads and trigger corresponding event handlers.

  • Expected Behavior: Binary messages should be automatically detected and parsed (e.g., via message->get_flag() == sio::message::flag_binary).
  • Actual Behavior: The registered event handler (on_message) is not triggered when binary messages are received, despite server-side confirmation of successful emission.

Reproduction Steps

  1. Server-Side (Node.js):
    socket.emit("binaryEvent", Buffer.from([0x01, 0x02, 0x03]));
  2. Client-Side (Cpp):
    void on_message(const sio::event& ev) {
        try{
            std::cout << "Received message" << (sio::message::flag_string == ev.get_message()->get_flag()) <<std::endl;
            if(ev.get_message()->get_flag() == sio::message::flag_binary) {
                std::cout << "Received binary message" << std::endl;
            }
            else {
                std::cout << "Unsupported message type:"<< ev.get_message()->get_flag() << std::endl;
            }
        }    
        catch (const std::exception& e) {
            std::cerr << e.what() << std::endl;
        }
    }
    h.socket()->on("binaryEvent", &on_message);
  3. Observed Result:
    No console output from on_message handler.

Proposed Workaround (Non-Elegant)

Through code inspection, the binary detection logic in packet::is_binary_message appears problematic:

bool packet::is_binary_message(string const& payload_ptr)
{
    return payload_ptr.size()>0 && payload_ptr[0] == frame_message;
}

To address this, I manually prepend the frame_message to the start of the message and subsequently exclude this frame_message during the parsing of the binary message in the on_message function. This approach has proven to be effective.

Core Questions

  1. Known Issue: Is this limitation a known issue with the socket.io-client-cpp library, or is it specific to my implementation?
  2. Recommendations: If this is indeed a genuine problem, how can it arise if the library is meant to fully support the Socket.IO protocol? Are there more effective solutions for handling binary messages without the need for manual tagging?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions