Skip to content

Commit 2f3e983

Browse files
committed
chore: initial docs
1 parent a347e80 commit 2f3e983

File tree

18 files changed

+10430
-0
lines changed

18 files changed

+10430
-0
lines changed

docs/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
.nuxt
3+
.output
4+
dist

docs/.npmrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
shamefully-hoist=true
2+
ignore-workspace-root-check=true
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Getting Started
2+
3+
CrossWS provides a cross-platform API to define well-typed WebSocket apps that can then be integrated into various WebSocket servers using built-in adapters.
4+
5+
Writing a realtime WebSocket server that can work in different javascript and WebSocket runtimes is challenging because there is no single standard for WebSocket servers. You often need to go into many details of diffrent API implementations and it also makes switching from one runtime costly. CrossWS is a solution to this!
6+
7+
## Installing Package
8+
9+
You can install crossws from [npm](https://npmjs.com/crossws):
10+
11+
::code-group
12+
13+
```sh [auto]
14+
npx nypm i crossws
15+
```
16+
17+
```sh [npm]
18+
npm install crossws
19+
```
20+
21+
```sh [Yarn]
22+
yarn add crossws
23+
```
24+
25+
```sh [pnpm]
26+
pnpm add crossws
27+
```
28+
29+
```sh [bun]
30+
bun add crossws
31+
```
32+
33+
::
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# WebSocket Hooks
2+
3+
Using WebSocket hooks API, you can define a WebSocket server that works across runtimes with same synax.
4+
5+
CrossWS provides a cross-platform API to define WebSocket servers. An implementation with these hooks works across runtimes without needing you to go into details of any of them (while you always have the power to control low-level hooks). You can only define the life-cycle hooks that you only need and only those will be called on runtime.
6+
7+
```ts
8+
import { defineWebSocketHooks } from "crossws";
9+
10+
const websocketHooks = defineWebSocketHooks({
11+
open(peer) {
12+
console.log("[ws] open", peer);
13+
},
14+
15+
message(peer, message) {
16+
console.log("[ws] message", peer, message);
17+
if (message.text().includes("ping")) {
18+
peer.send("pong");
19+
}
20+
},
21+
22+
close(peer, event) {
23+
console.log("[ws] close", peer, event);
24+
},
25+
26+
error(peer, error) {
27+
console.log("[ws] error", peer, error);
28+
},
29+
30+
// ... platform hooks such as bun:drain ...
31+
});
32+
```
33+
34+
> [!NOTE]
35+
> Using `defineWebSocketHooks` to define hooks, we have type support and IDE auto-completion even if not using typescript. This utility does nothing more and you can use a plain object as well if you prefer to.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# WebSocket Peer
2+
3+
Peer object allows easily interacting with connected clients.
4+
5+
Almost all Websocket hooks accept a peer instance as their first argument. You can use peer object to get information about each connected client or a message to them.
6+
7+
> [!TIP]
8+
> You can safely log a peer instance to the console using `console.log` it will be automatically stringified with useful information including the remote address and connection status!
9+
10+
## API Reference
11+
12+
### `peer.send(message, compress)`
13+
14+
Send a message to the connected client
15+
16+
### `peer.id`
17+
18+
The peer address or unique id (might be `undefined`)
19+
20+
### `peer.readyState`
21+
22+
Client connection status (might be `undefined`)
23+
24+
(read more in [MDN](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState))
25+
26+
### `peer.ctx`
27+
28+
`peer.ctx` is an object that holds adapter specific context. It is scoped with `peer.ctx.[name]`.
29+
30+
> [!NOTE]
31+
> This is an advanced namespace and API changes might happen, so don't rely on it as much aspossible!
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# WebSocket Message
2+
3+
On `message` hook, you receive a message object containing an incoming message from the client.
4+
5+
> [!TIP]
6+
> You can safely log `message` object to the console using `console.log` it will be automatically stringified!
7+
8+
## API Reference
9+
10+
### `message.text()`
11+
12+
Get stringified text version of the message
13+
14+
### `message.rawData`
15+
16+
Raw message data
17+
18+
### `message.isBinary`
19+
20+
Indicates if the message is binary (might be `undefined`)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# WebSocket Client
2+
3+
Universal WebSocket API to connect to WebSocket servers from clients.
4+
5+
CrossWS exposes [`WebSocket`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) in order to connect to the server.
6+
7+
For all runtimes, except Node.js, native implementation is used, and for Node.js, a bundled version of [`ws.WebSocket`](https://www.npmjs.com/package/ws) is bundled.
8+
9+
```js
10+
import WebSocket from "crossws/websocket";
11+
```
12+
13+
> [!NOTE]
14+
> Using export conditions, the correct version will be always used so you don't have to worry about picking the right build!
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Runtime Adapters
2+
3+
CrossWS allows integrating your WebSocket hooks with different runtimes and platforms using built-in adapters. Each runtime has a specific method of integrating WebSocket. Once integrated, will work consistently even if you change the runtime.
4+
5+
See Adapters section to learn more about all available built-in adapters
6+
7+
## Integration with other runtimes
8+
9+
You can define your custom adapters using `defineWebSocketAdapter` wrapper.
10+
11+
See other adapter implementations in [`src/adapters`](https://github.com/unjs/crossws/tree/main/src/adapters/) to get an idea of how adapters can be implemented and feel free to directly make a Pull Request to support your environment in CrossWS!

docs/content/3.adapters/bun.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Bun
2+
3+
Integrate CrossWS with Bun.
4+
5+
To integrate CrossWS with your Bun server, you need to check for `server.upgrade` and also pass the `websocket` object returned from the adapter to server options. CrossWS leverages native Bun WebSocket API.
6+
7+
```ts
8+
import wsAdapter from "./dist/adapters/bun";
9+
10+
const { websocket } = wsAdapter({ message: console.log });
11+
12+
Bun.serve({
13+
port: 3000,
14+
websocket,
15+
fetch(req, server) {
16+
if (server.upgrade(req)) {
17+
return;
18+
}
19+
return new Response(
20+
`<script>new WebSocket("ws://localhost:3000").addEventListener('open', (e) => e.target.send("Hello from client!"));</script>`,
21+
{ headers: { "content-type": "text/html" } },
22+
);
23+
},
24+
});
25+
```
26+
27+
## Adapter Hooks
28+
29+
- `bun:message (peer, ws, message)`
30+
- `bun:open (peer, ws)`
31+
- `bun:close (peer, ws)`
32+
- `bun:drain (peer)`
33+
- `bun:error (peer, ws, error)`
34+
- `bun:ping (peer, ws, data)`
35+
- `bun:pong (peer, ws, data)`
36+
37+
## Learn More
38+
39+
See [`playground/bun.ts`](https://github.com/unjs/crossws/tree/main/playground/bun.ts) for demo and [`src/adapters/bun.ts`](https://github.com/unjs/crossws/tree/main/src/adapters/bun.ts) for implementation.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Cloudflare
2+
3+
Integrate CrossWS with Cloudflare Workers.
4+
5+
To integrate CrossWS with your Cloudflare Workers, you need to check for the `upgrade` header.
6+
7+
```ts
8+
import wsAdapter from "crossws/adapters/cloudflare";
9+
10+
const { handleUpgrade } = wsAdapter({ message: console.log });
11+
12+
export default {
13+
async fetch(request, env, context) {
14+
if (request.headers.get("upgrade") === "websocket") {
15+
return handleUpgrade(request, env, context);
16+
}
17+
return new Response(
18+
`<script>new WebSocket("ws://localhost:3000").addEventListener("open", (e) => e.target.send("Hello from client!"));</script>`,
19+
{ headers: { "content-type": "text/html" } },
20+
);
21+
},
22+
};
23+
```
24+
25+
## Adapter Hooks
26+
27+
- `cloudflare:accept (peer)`
28+
- `cloudflare:message (peer, event)`
29+
- `cloudflare:error (peer, event)`
30+
- `cloudflare:close (peer, event)`
31+
32+
## Learn More
33+
34+
See [`playground/cloudflare.ts`](https://github.com/unjs/crossws/tree/main/playground/cloudflare.ts) for demo and [`src/adapters/cloudflare.ts`](https://github.com/unjs/crossws/tree/main/src/adapters/cloudflare.ts) for implementation.

0 commit comments

Comments
 (0)