zeromq / libzmq

ZeroMQ core engine in C++, implements ZMTP/3.1
https://www.zeromq.org
Mozilla Public License 2.0
9.64k stars 2.35k forks source link

V4.3.4 websocket can't communicate with JS native websocket. #4152

Open jiazhanfeng1989 opened 3 years ago

jiazhanfeng1989 commented 3 years ago

Issue description

I wrote a simple websocket server using ZeroMQ. I tried to use JS native websocket to communicate with the server, but failed in message handshake. But the websocket client, written in ZeroMQ works fine.

Minimal test code / Steps to reproduce the issue

c++ side:

int main()
{
    std::string addr = "ws://127.0.0.1:6378";
    zmq::context_t context(1);
    zmq::socket_t frontend(context, ZMQ_DEALER);

    frontend.bind(addr);

    while (1)
    {
        zmq::message_t message;
        frontend.recv(message);
        std::cout << "recive data:" << message.to_string() << std::endl;
        message.rebuild();

        std::string data = "hello world";
        frontend.send(data.data(), data.size());
        std::cout << "continue..." << std::endl;
    }
}

js side:

var ws = new WebSocket("ws://localhost:6378"); 

What's the actual result? (include assertion message & call stack if applicable)

js: WebSocket connection to 'ws://127.0.0.1:6378' failed: Connection closed before receiving a handshake response

What's the expected result?

js native websocket works fine

jiazhanfeng1989 commented 3 years ago

ref: https://rfc.zeromq.org/spec/45/

stvales commented 3 years ago

The problem comes from the fact that, from a websocket perpsective, only the ZWS 2.0 subprotocol is supported by ZMQ at the moment. ZMQ immediately breaks the connection with standard websocket clients which do not list this subprotocol during the handshake.

The right way to communicate with native websocket clients/servers should be with a STREAM socket (you are using a DEALER in your example), exactly like with the TCP transport and native TCP sockets. As soon as one uses a specific ZMQ sockets , ZWS 2.0 is the way to go and JSZMQ shall be used in web-based clients.

+1 on your request : it would be great to extend libzmq so that STREAM sockets properly support the ws/wss transport with native websockets.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had activity for 365 days. It will be closed if no further activity occurs within 56 days. Thank you for your contributions.