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

Subcription for a GraphQL server using graphql-transport-ws does not work #59

Closed
murilodag opened this issue Dec 10, 2023 · 1 comment
Closed

Comments

@murilodag
Copy link

I am attempting to run a k6 test using subscriptions. I attempted using both k6/ws and k6/experimental/websocket with same result.
When the code below is executed, the websocket is opened, the code sends connection_init message and nothing else works (no reply from server).
When the code below is executed without connection_init, the Ping/Pong sequence happens for a few seconds until timeout.
Full logs can be found below.

On top of this, there is also a subscription flow to retrieve data from the server (hidden within executeFlow). With/without it, the results are the same and nothing is returned to the load test.

    let messages = [];
    const headers = {
        'Sec-Websocket-Protocol': 'graphql-transport-ws',
    };
    const socket = ws.WebSocket(url, null, { headers });
    socket.onopen = function open() {
        if (debug) console.log('Websocket opened');
        socket.send(JSON.stringify({
            "type": "connection_init",
            "payload": { "x-api-key": xApiKey }
        }));
    };

    socket.onmessage = function (message) {
        const data = JSON.parse(message.data);

        if (data.type == 'connection_ack') {
            if (debug) console.log('Websocket connected');
            executeFlow(socket, messages);
        } else {
            if (debug) console.log('Websocket new message', message);
            messages.push(message);
        }
    };

    socket.onclose = () => {
        if (debug) console.log('Websocket closed');
    };

    socket.onerror = (e) => {
        if (e.error != 'websocket: close sent') {
            console.log('Websocket error: ', e.error);
        }
    };

    setInterval(function () {
        console.log('Ping!');
        socket.ping();
    }, 1000);

    socket.onpong = function () {
        console.log('Pong!');
    };

Logs from execution 1

INFO[0000] Websocket opened                              source=console
INFO[0001] Ping!                                         source=console
INFO[0001] Pong!                                         source=console
INFO[0002] Ping!                                         source=console
INFO[0002] Pong!                                         source=console
INFO[0003] Ping!                                         source=console
INFO[0003] Pong!                                         source=console
INFO[0003] Websocket error:  websocket: close 4408: Connection initialisation timeout  source=console
INFO[0003] Websocket closed                              source=console
INFO[0004] Ping!                                         source=console
WARN[0004] setInterval 1 was stopped because the VU iteration was interrupted 
ERRO[0004] GoError: InvalidStateError
running at github.com/grafana/xk6-websockets/websockets.(*webSocket).ping-fm (native)

Logs from execution 2

INFO[0000] Websocket opened                              source=console
INFO[0001] Send ping!                                    source=console
INFO[0001] Pong!                                         source=console
INFO[0002] Send ping!                                    source=console
INFO[0002] Pong!                                         source=console
INFO[0003] Send ping!                                    source=console
INFO[0003] Pong!                                         source=console
INFO[0003] Websocket error:  websocket: close 4408: Connection initialisation timeout  source=console
INFO[0003] Websocket closed                              source=console
INFO[0004] Send ping!                                    source=console
WARN[0004] setInterval 1 was stopped because the VU iteration was interrupted 
ERRO[0004] GoError: InvalidStateError
running at github.com/grafana/xk6-websockets/websockets.(*webSocket).ping-fm (native)
@codebien
Copy link
Contributor

codebien commented Dec 19, 2023

Hey @murilodag,
I don't see any specific error in your script except the missing call to close method, but it shouldn't be relevant for the current context.

It seems to me you're not implementing the handshake as expected from the protocol. You should check the server logs and see what is missing.

As you can read from the protocol's documentation the error you're receiving means the server doesn't receive the expected message for establishing a connection.

When the code below is executed, the websocket is opened, the code sends connection_init message and nothing else works (no reply from server).

I'm not understanding if it is able to reach the line if (debug) console.log('Websocket connected'); in this case. Do you see the print?

Did you try to establish the connection using another client? Like a classic javascript script running in the browser. The current issue doesn't sound to be related to k6 so we may not help you concretely.

@codebien codebien removed their assignment Dec 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants