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

Latest version doesn't work with Cloudflare Workers #1127

Closed
Braden1996 opened this issue Feb 12, 2023 · 5 comments · Fixed by #1132
Closed

Latest version doesn't work with Cloudflare Workers #1127

Braden1996 opened this issue Feb 12, 2023 · 5 comments · Fixed by #1132

Comments

@Braden1996
Copy link

Braden1996 commented Feb 12, 2023

Error occurred on version 1.2.36:

CleanShot 2023-02-12 at 17 37 02

Importing from ably/build/ably-webworker.min gives me:
CleanShot 2023-02-12 at 17 53 56

Temporarily downgrading to 1.2.4 resolves the issue, but I am still unable to publish to channels.

@sync-by-unito
Copy link

sync-by-unito bot commented Feb 12, 2023

➤ Automation for Jira commented:

The link to the corresponding Jira issue is https://ably.atlassian.net/browse/SDK-3340

@owenpearson
Copy link
Member

Hey @Braden1996, thanks for reporting this! Before we look into this I just want to check, did you mean to reopen the issue with that last comment?

@Braden1996
Copy link
Author

Braden1996 commented Feb 26, 2023

Hey @Braden1996, thanks for reporting this! Before we look into this I just want to check, did you mean to reopen the issue with that last comment?

Hey! Sorry, this issue still seems to be happening. Importing from ably instantly crashes a Cloudflare Worker server, as it tries to access window. I tried importing from ably/build/ably-webworker.min, which gets me further but still doesn't work (see below).

import type { Types } from "ably";
import AblyWebWorker from "ably/build/ably-webworker.min";  // Types don't seem to work here.

export default class Ably {
  private ably: Types.RestPromise;

  constructor(ablyApiKey: string) {
    const clientOptions: Types.ClientOptions = {
      key: ablyApiKey,
    };
    this.ably = new AblyWebWorker.Rest.Promise(clientOptions);
  }

  // This works
  async createTokenRequest(userId: string) {
    const tokenRequest = await this.ably.auth.createTokenRequest({
      clientId: userId,
      capability: {
        [`user:${userId}`]: ["subscribe", "publish"],
      },
    });
    return tokenRequest;
  }

  // This does not work :(
  async publishToChannel(
    channelName: string,
    messageName: string,
    messagePayload: string
  ) {
    const channel = this.ably.channels.get(channelName);
    await channel.publish(messageName, messagePayload);
  }
}

In the example code, the publishToChannel method errors with:

Error: The 'credentials' field on 'RequestInitializerDict' is not implemented.

@Braden1996
Copy link
Author

Braden1996 commented Feb 26, 2023

For the time being, I'm getting round this by:

  async publishToChannel(
    channelName: string,
    messageName: string,
    messagePayload: string
  ) {
    const encodedChannelName = encodeURIComponent(channelName);
    const b64Key = btoa(this.ablyApiKey);
    fetch(`https://rest.ably.io/channels/${encodedChannelName}/messages`, {
      method: "POST",
      headers: {
        Authorization: `Basic ${b64Key}`,
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        name: messageName,
        data: messagePayload,
      }),
    });
  }

@owenpearson
Copy link
Member

Hey @Braden1996 , we've updated the library in 1.2.37 to not send the credentials field when using the webworker version. I hope this helps and please let us know if you have any further issues :)

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

Successfully merging a pull request may close this issue.

2 participants