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
> Integrate crossws with Cloudflare Workers and Durable Objects.
8
8
9
-
To integrate crossws with your Cloudflare Workers, you need to check for the `upgrade` header.
9
+
To integrate crossws with Cloudflare [Durable Objects](https://developers.cloudflare.com/durable-objects/api/websockets/) with [pub/sub](/guide/pubsub) and [hibernation API](https://developers.cloudflare.com/durable-objects/best-practices/websockets/#websocket-hibernation-api) support, you need to check for the `upgrade` header and additionally export a DurableObject with crossws adapter hooks integrated.
10
10
11
-
> [!IMPORTANT]
12
-
> For [pub/sub](/guide/pubsub) support, you need to use [Durable objects](#durable-objects).
13
-
14
-
```ts
15
-
importcrosswsfrom"crossws/adapters/cloudflare";
16
-
17
-
const ws =crossws({
18
-
hooks: {
19
-
message: console.log,
20
-
},
21
-
});
22
-
23
-
exportdefault {
24
-
async fetch(request, env, context) {
25
-
if (request.headers.get("upgrade") ==="websocket") {
26
-
returnws.handleUpgrade(request, env, context);
27
-
}
28
-
returnnewResponse(
29
-
`<script>new WebSocket("ws://localhost:3000").addEventListener("open", (e) => e.target.send("Hello from client!"));</script>`,
30
-
{ headers: { "content-type": "text/html" } },
31
-
);
32
-
},
33
-
};
34
-
```
35
-
36
-
::read-more
37
-
See [`test/fixture/cloudflare.ts`](https://github.com/h3js/crossws/blob/main/test/fixture/cloudflare.ts) for demo and [`src/adapters/cloudflare.ts`](https://github.com/h3js/crossws/blob/main/src/adapters/cloudflare.ts) for implementation.
38
-
::
39
-
40
-
## Durable objects
41
-
42
-
To integrate crossws with Cloudflare [Durable Objects](https://developers.cloudflare.com/durable-objects/api/websockets/) (available on paid plans) with pub/sub and hibernation support, you need to check for the `upgrade` header and additionally export a Durable object with crossws adapter hooks integrated.
11
+
> [!NOTE]
12
+
> If you skip durable object class export or in cases the binding is unavailable, crossws uses a **fallback mode** without pub/sub support in the same worker.
See [`test/fixture/cloudflare-durable.ts`](https://github.com/h3js/crossws/blob/main/test/fixture/cloudflare-durable.ts) for demo and [`src/adapters/cloudflare-durable.ts`](https://github.com/h3js/crossws/blob/main/src/adapters/cloudflare-durable.ts) for implementation.
75
+
See [`test/fixture/cloudflare-durable.ts`](https://github.com/h3js/crossws/blob/main/test/fixture/cloudflare-durable.ts) for demo and [`src/adapters/cloudflare.ts`](https://github.com/h3js/crossws/blob/main/src/adapters/cloudflare.ts) for implementation.
0 commit comments