Search code examples
gowebsocketsocket.iogorilla

Socket.io client not working with websocket server


I wrote a websocket server in go using https://github.com/gorilla/websocket. On the client the front-end team want's to use Socket.io, and is not working. With plain websockets everything works fine. I don't have a lot of knowledge about Socket.io, just what I read on internet, and I didn't found any decisive and recent answer.

Is there any possibility to make Socket.io to work with a normal websocket server?

Socket.IO Client Error: Timeout .
Socket.IO Client Code:

    var token = "valid_token";

    var socket = io(
        'ws://localhost:6060',
        {
            transports: ['websocket'],
            path: '/ws/dispatchers',
            query:  {
                token: token
            },
        }
    );

    socket.on('connect_error', (error) => {
        console.log("------connect_error------");
        console.log(error);
    });

    socket.on('connect_timeout', (timeout) => {
        console.log("------timeout-----");
        console.log(timeout);
    });

    socket.on('connect', function(){
        console.log("---------connect---------");
    });
    socket.on('event', function(data){
        console.log(data)
    });
    socket.on('disconnect', function(){
        console.log("---------disconnect---------")
    });
    socket.on('error', function(err){
        console.log("--------error------------");
        console.log(err)
    });

    socket.on('ping', () => {
        console.log("--------ping------------");

    });

    socket.on('pong', (latency) => {
        console.log("--------pong------------");
    });

Backend Error: websocket: close 1005 (no status)
Backend code, is the same as: https://github.com/gorilla/websocket/tree/master/examples/chat


Solution

  • From Socket.io Documentation

    What Socket.IO is not Socket.IO is NOT a WebSocket implementation. Although Socket.IO indeed uses WebSocket as a transport when possible, it adds some metadata to each packet: the packet type, the namespace and the packet id when a message acknowledgement is needed. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a WebSocket server either. Please see the protocol specification here.