You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using ably-js in Chrome extensions, you may encounter CORB (Cross-Origin Resource Blocking) errors, particularly in Chrome 73 and later versions.
48
-
49
-
#### The problem
50
-
51
-
Chrome 73+ made changes to how cross-origin requests from content scripts work. Content scripts are now subject to stricter rules than regular page scripts:
52
-
53
-
- Content scripts don't trigger CORS preflight requests for cross-origin requests
54
-
- This causes the main request to be blocked by CORB
55
-
- The error typically appears as: `"CORB blocked cross-origin response"`
56
-
57
-
#### Solutions
58
-
59
-
**Recommended: Use background script**
60
-
61
-
The most reliable solution is to run the Ably library in the extension's background script rather than the content script.
62
-
63
-
**Alternative: Content script with workarounds**
64
-
65
-
If you need to use the library in a content script, implement these workarounds:
66
-
67
-
1.**Use WebSocket-only transport** to avoid HTTP requests:
68
-
69
-
```javascript
70
-
constably=newAbly.Realtime({
71
-
key:'your-api-key',
72
-
transports: ['web_socket'] // Avoids XHR transport that triggers CORB
73
-
});
74
-
```
75
-
76
-
2.**Implement custom authCallback** for token authentication:
77
-
78
-
```javascript
79
-
constably=newAbly.Realtime({
80
-
authCallback: (tokenParams, callback) => {
81
-
// Send message to background script to fetch token
82
-
chrome.runtime.sendMessage({
83
-
action:'getAblyToken',
84
-
tokenParams: tokenParams
85
-
}, (response) => {
86
-
if (response.error) {
87
-
callback(response.error);
88
-
} else {
89
-
callback(null, response.token);
90
-
}
91
-
});
92
-
},
93
-
transports: ['web_socket']
94
-
});
95
-
```
96
-
97
-
3.**Ensure your auth server uses `requestToken`** rather than `createTokenRequest` to avoid additional REST requests from the client.
98
-
99
-
<Asidedata-type="important">
100
-
When using authCallback with background script communication, make sure your auth server calls `requestToken` rather than `createTokenRequest`. Otherwise, the library will attempt additional REST requests that may be blocked by CORB.
101
-
</Aside>
102
-
103
-
For more details, see the [Chromium documentation on extension content script fetches](https://www.chromium.org/Home/chromium-security/extension-content-script-fetches).
104
-
105
45
</If>
106
46
107
47
<Iflang="nodejs">
@@ -165,44 +105,6 @@ repositories {
165
105
```
166
106
</Code>
167
107
168
-
### ProGuard configuration <aid="proguard"/>
169
-
170
-
If you're using ProGuard for code obfuscation in your Java or Android project, you may encounter `ClassNotFoundException` errors when trying to use Ably. This happens because ProGuard may remove classes that are accessed via reflection.
171
-
172
-
#### The problem
173
-
174
-
ProGuard obfuscation can remove or rename classes that Ably depends on, particularly:
175
-
176
-
- Ably internal classes that are accessed dynamically
177
-
- Third-party dependencies like MessagePack serialization classes
Add the following keep rules to your ProGuard configuration file (`proguard-rules.pro`):
188
-
189
-
```proguard
190
-
# Keep Ably classes
191
-
-keep class io.ably.lib.** { *; }
192
-
193
-
# Keep MessagePack classes (used by Ably for serialization)
194
-
-keep class org.msgpack.core.** { *; }
195
-
```
196
-
197
-
These rules ensure that:
198
-
199
-
1.**Ably classes**: All classes in the `io.ably.lib` package are preserved, preventing issues with dynamic class loading
200
-
2.**MessagePack classes**: Serialization classes used internally by Ably are kept intact
201
-
202
-
<Asidedata-type="note">
203
-
If you're using a minimal ProGuard configuration and still encounter issues, you may need to add additional keep rules for other dependencies. Check your ProGuard logs for specific classes that are being removed.
0 commit comments