Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Uncaught Error occurs when connection fails with WebSocket #1839

Closed
ogis-yamazaki opened this issue Apr 9, 2024 · 2 comments · Fixed by fvilarinho/akamai-siem-connector#35 or fvilarinho/akamai-siem-connector#39
Labels

Comments

@ogis-yamazaki
Copy link
Contributor

ogis-yamazaki commented Apr 9, 2024

MQTTjs Version

5.5.0

Broker

none

Environment

Browser (Google Chrome 122.0.6261.112)
nginx

Description

I was trying a test that intentionally generated an error.
No broker was used, only nginx was used.

I have nginx listening on port 80 and when I try to connect to it via websocket I get Uncaught Error.
The expected error event does not seem to occur.

From my research, it seems to be affected by the following new part that was added in 5.5.0.
However, I do not know how it should be fixed.

https://github.com/mqttjs/MQTT.js/blob/9fc79dfc855882558450d433b71092176921d634/src/lib/BufferedDuplex.ts#L82

Minimal Reproduction

Reproduction code

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-Control" content="no-cache">
    <title>issue</title>
</head>
<body>
    <script src="https://unpkg.com/mqtt@5.5.0/dist/mqtt.js"></script>
    <script>
        const connectOptions = {
            keepalive: 300,
            clean: true,
            clientId: 'userA',
            reconnectPeriod: 0
        }

        const uri = 'ws://127.0.0.1:80'
        const client = mqtt.connect(uri, connectOptions);

        client.on('close', () => {
            console.info(`close event`)
        });
        client.on('error', (err) => {
            console.info(`error event. err = ${err}`)
        });
        
        client.on('connect', (packet) => {
            console.info(`connect event. packet = ${JSON.stringify(packet)}`)
        });
    </script>
</body>
</html>

Here is my nginx.conf

#user http;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
}

Debug logs

console
WebSocket connection to 'ws://127.0.0.1/' failed:
Uncaught Error: WebSocket error

@robertsLando
Copy link
Member

Hi @ogis-yamazaki, when destroy is called and err is not undefined the stream also emits an error event and by defautl if no error listener is attached that results in an uncaught exception. In this case we could both add an error listener on proxy or simply ignore the error there and just return the error to the callback, I would go for the second case for now because it makes more sense to me

@ogis-yamazaki
Copy link
Contributor Author

@robertsLando

Thank you fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants