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

feat: implement messages callbacks for channels #9

Merged
merged 1 commit into from Oct 15, 2022

Conversation

SlayerDF
Copy link
Contributor

@SlayerDF SlayerDF commented Sep 29, 2022

Closes #2

@skryukov
Copy link
Member

Hey @SlayerDF, thanks for the PR! ❤️

It looks like just what we need, the only thing we are missing is providing an example and docs in the readme.

Do you want to add them? If not, I will just merge this PR and add it myself.

Here is what I used to test this PR, btw:

import { check, fail, sleep } from "k6";
import cable from "k6/x/cable";

let userId = `100${__VU}`;
let userName = `Kay${userId}`;

const URL = __ENV.CABLE_URL || "ws://localhost:8080/cable";
const WORKSPACE = __ENV.WORKSPACE || "demo";

export default function () {
    let client = cable.connect(URL, {
        cookies: `uid=${userName}/${userId}`,
        receiveTimeoutMS: 30000,
    });

    if (
        !check(client, {
            "successful connection": (obj) => obj,
        })
    ) {
        fail("connection failed");
    }

    const channel = client.subscribe("ChatChannel", { id: WORKSPACE });

    if (
        !check(channel, {
            "successful subscription": (obj) => obj,
        })
    ) {
        fail("failed to subscribe");
    }

    channel.onMessage((msg) => {
        check(msg, {
            "Hello message received": (obj) => obj.message.html.includes('Hello'),
        });
    });

    channel.perform("speak", { message: "Hello" });
    channel.perform("speak", { message: "Hello" });
    // this will fail
    channel.perform("speak", { message: "Bye" });

    sleep(1);

    // Terminate the WS connection
    client.disconnect()
}

@SlayerDF
Copy link
Contributor Author

@skryukov
I'm sorry I forgot about the docs. Nevertheless I would appreciate if you add it yourself 😌

@skryukov skryukov merged commit 129fa55 into anycable:master Oct 15, 2022
This was referenced Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Asynchronous channel.onMessage(callback) method
2 participants