socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

invalid payload error on socket_error event

Open MuhammedAlmaz opened this issue 9 months ago • 3 comments

Describe the bug I am trying to implementation android and nodejs server. When i am connect to server from android device i am getting invalid payload after 5-6 sec. I tried many things. I've checked https://github.com/socketio/socket.io/issues/4392 issue but there is not any solve way. I can see the first emit when i connect to server but after some sec later my connection closing.

This issue is not happening all clients. Only some of them about %10 of clients.

I have an apache server which is reverse proxying to my socket.

The config of apache server

<VirtualHost _default_:443>
        ServerName socket.xxxx.com
        SSLEngine On
        SSLCertificateFile /etc/ssl/socket/primary.crt
        SSLCertificateKeyFile /etc/ssl/socket/private.key
        SSLCertificateChainFile /etc/ssl/socket/intermediate.crt


        LoadModule authn_file_module            modules/mod_authn_file.so
        LoadModule authn_core_module            modules/mod_authn_core.so
        LoadModule authz_host_module            modules/mod_authz_host.so
        LoadModule authz_groupfile_module       modules/mod_authz_groupfile.so
        LoadModule authz_user_module            modules/mod_authz_user.so
        LoadModule authz_core_module            modules/mod_authz_core.so
        LoadModule headers_module               modules/mod_headers.so
        LoadModule lbmethod_byrequests_module   modules/mod_lbmethod_byrequests.so
        LoadModule proxy_module                 modules/mod_proxy.so
        LoadModule proxy_balancer_module        modules/mod_proxy_balancer.so
        LoadModule proxy_http_module            modules/mod_proxy_http.so
        LoadModule proxy_wstunnel_module        modules/mod_proxy_wstunnel.so
        LoadModule rewrite_module               modules/mod_rewrite.so
        LoadModule slotmem_shm_module           modules/mod_slotmem_shm.so
        
        SSLProxyEngine on
        SSLProxyVerify none
        Header set Access-Control-Allow-Origin "*"
        ProxyPass / http://localhost:9252/
        RewriteEngine on
        RewriteCond %{HTTP:Upgrade} websocket [NC]
        RewriteCond %{HTTP:Connection} upgrade [NC]
        RewriteRule ^/?(.*) "ws://localhost:9252/$1" [P,L]
        
        ProxyTimeout 30
    </VirtualHost>

Server side socket.io version is: "socket.io": "4.7.2" Android side socket.io-client version is : implementation 'io.socket:socket.io-client:2.1.0' Android Version: [8,9,10,11,12,13]

Server side codes

    socket.on('error', (error) => {
      console.log(`${moment().format('DD-MM-YYYY HH:mm:ss')} -> socket error ${socket.id} ${socket.handshake.query.connectionType} ${socket.room}`, error);
    });
    
    socket.on('disconnect', (reason, details) => {
      console.log(`Disconnected ${moment().format('DD-MM-YYYY HH:mm:ss')} -> ${socket?.room} - ${socket?.id} - ${reason} ${details}`);
    });

Error information

19-09-2023  20:50:25 -> socket error _rO9bl3lGaNKXIQ0AAAh 2 77 Error: invalid payload
    at Decoder.decodeString (/projects/prod_socket/node_modules/socket.io-parser/build/cjs/index.js:238:23)
    at Decoder.add (/projects/prod_socket/node_modules/socket.io-parser/build/cjs/index.js:141:27)
    at Client.ondata (/projects/prod_socket/node_modules/socket.io/dist/client.js:182:26)
    at Socket.emit (events.js:400:28)
    at Socket.onPacket (/projects/prod_socket/node_modules/engine.io/build/socket.js:122:22)
    at WebSocket.emit (events.js:400:28)
    at WebSocket.onPacket (/projects/prod_socket/node_modules/engine.io/build/transport.js:92:14)
    at WebSocket.onData (/projects/prod_socket/node_modules/engine.io/build/transport.js:101:14)
    at WebSocket.<anonymous> (/projects/prod_socket/node_modules/engine.io/build/transports/websocket.js:20:19)
    at WebSocket.emit (events.js:400:28)
    
    
    Disconnected 19-09-2023  20:50:28 -> 77 - _rO9bl3lGaNKXIQ0AAAh - forced close undefined

MuhammedAlmaz avatar Sep 19 '23 18:09 MuhammedAlmaz

I tried to collect more information about bug.

And i find a thing. This event firing that error payload thing. I edited the server code and find an event.

The code line is : /projects/prod_socket/node_modules/socket.io-parser/build/cjs/index.js:238:23 image

and the console output is: null { type: 2, nsp: '/', id: 3186, data: null }

Data is null idk why there is null event but i find that. Maybe it will help to solve that issue.

Android connection code is;

            IO.Options opts = new IO.Options();
            opts.secure = true;
            opts.reconnectionDelay = 5000;
            opts.timeout = 6000;
            opts.transports = new String[]{WebSocket.NAME, Polling.NAME};
            opts.query = "token=" + token + "&&connectionType=2";
            webSocket = IO.socket(this.socketUri, opts);

It seems like ping, pong request but i am not sure...

MuhammedAlmaz avatar Sep 19 '23 19:09 MuhammedAlmaz

I found the bug i am not sure is it from client library or server but the problem is happening from "Ack Callback".


        webSocket.emit("client.register-room", username, new Ack() {
            @Override
            public void call(Object... args) {
                isRegistered=true;
            }
        });

When i run this method server closing socket and firing invalid payload error.

It is really interesting. Did not anybody use ACK callback on Java Client?

MuhammedAlmaz avatar Sep 20 '23 07:09 MuhammedAlmaz

Hi! Thanks a lot for all the details.

In the payload { type: 2, nsp: '/', id: 3186, data: null }, the data: null is indeed invalid (it should be an array).

Not sure how it could happen though.

darrachequesne avatar Sep 20 '23 08:09 darrachequesne