-
Notifications
You must be signed in to change notification settings - Fork 55
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
ReferenceError: window is not defined
when initialising Realtime in a Service Worker
#1013
Comments
ReferenceError: window is not defined
when initialising Realtime in a Service Worker
Hi @snehalbaghel, thanks for reporting this. You're right that the docs are out of date for this, I've had a go at reproducing the error you're seeing myself but haven't been able to yet. My attempt to reproduce used this source code (compiled with webpack and ts-loader): import Ably from 'ably/build/ably-webworker.min';
const client = new Ably.Realtime({ key: 'xxx' });
client.connection.on('connected', () => {
console.log('connected');
}); And this extension manifest: {
"name": "Ably extension",
"description": "Used to test using ably-js in a chrome extension service worker",
"version": "1.0",
"manifest_version": 3,
"permissions": [
"activeTab",
"scripting"
],
"background": {
"service_worker": "background.js"
}
} When I load the extension the library logs out Could you provide some more detailed instructions on how to reproduce the error you're seeing? |
Hey @owenpearson thanks for the quick response.
I'm using Plasmo which has a parcel bundler, it is possible the issue is because of that. I've created a repo to reproduce the error here: https://github.com/snehalbaghel/ably-sw-example, can you see if you're able to reproduce it with this setup? |
Thanks @snehalbaghel, that's really helpful. I was able to reproduce the issue using your repo and have opened a PR which fixes it. I'm planning on cutting a new ably-js release either today or tomorrow so you should be fine just using the new version once it's released. Until then, you're welcome to pull down the |
Thanks @owenpearson! I'll wait for the new version to be released. Thanks for resolving it so promptly! |
Hey @snehalbaghel, just to let you know, we've released version 1.2.26. I've just tested it with your demo repo and it's working as expected now 👍 |
@owenpearson Thanks! I tried it and it seems to create the client without throwing any errors. I'm getting issue with auth now however:
Here's the error object that I'm seeing:
I'm able to reproduce it in the same repo. I also tried calling authorize manually but didn't see any request in the network panel:
|
This error response indicates that the library encountered an error when it attempted to make a request to the Perhaps the first thing to do would be to enable verbose logging so we can see exactly what request the library attempted to make against the |
I've reproduced the issue locally. The problem here is that ably-js internally uses the XMLHttpRequest API for HTTP requests but this API isn't available in the service worker JS environment (same issue as #766). I'm currently looking into adding the fetch API as a fallback. |
@paddybyers Here are the logs:
I did not see any request going through in the network tab so it is possible that the attempt failed. I checked our server logs as well but did not find any requests that responded with 500 or 401. Is the library using xhr to make the request as that may not work with service workers. |
@snehalbaghel While I'm working on fetch API support, a workaround here would be to use the Obviously the implementation will depend on your auth endpoint but example usage would look like this: new Ably.Realtime({
authCallback: (tokenParams, callback) => {
fetch(url, {
method: 'POST',
body: JSON.stringify(tokenParams),
}).then(res => {
if (!res.ok) {
res.text().then(body => {
callback(new Error(`authCallback request failed. body = ${body}`));
})
} else {
res.json().then(tokenDetails => {
callback(null, tokenDetails);
});
}
});
},
}); |
@owenpearson Oh that's nice, I did not know about |
Version:
1.2.25
I'm trying to initialise a realtime connection in a chrome extension Service Worker using
ably-webworker.min
I'm importing it like so:On creating a realtime object the following error is thrown:
The docs for web workers says that I need to import ably from
'ably/browser/static/ably-webworker.min'
however there was no such directory in the package so I ended up using'ably/build/ably-webworker.min'
which is throwing this error.┆Issue is synchronized with this Jira Uncategorised by Unito
The text was updated successfully, but these errors were encountered: