-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
10,430 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
.nuxt | ||
.output | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
shamefully-hoist=true | ||
ignore-workspace-root-check=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Getting Started | ||
|
||
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. | ||
|
||
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! | ||
|
||
## Installing Package | ||
|
||
You can install crossws from [npm](https://npmjs.com/crossws): | ||
|
||
::code-group | ||
|
||
```sh [auto] | ||
npx nypm i crossws | ||
``` | ||
|
||
```sh [npm] | ||
npm install crossws | ||
``` | ||
|
||
```sh [Yarn] | ||
yarn add crossws | ||
``` | ||
|
||
```sh [pnpm] | ||
pnpm add crossws | ||
``` | ||
|
||
```sh [bun] | ||
bun add crossws | ||
``` | ||
|
||
:: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# WebSocket Hooks | ||
|
||
Using WebSocket hooks API, you can define a WebSocket server that works across runtimes with same synax. | ||
|
||
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. | ||
|
||
```ts | ||
import { defineWebSocketHooks } from "crossws"; | ||
|
||
const websocketHooks = defineWebSocketHooks({ | ||
open(peer) { | ||
console.log("[ws] open", peer); | ||
}, | ||
|
||
message(peer, message) { | ||
console.log("[ws] message", peer, message); | ||
if (message.text().includes("ping")) { | ||
peer.send("pong"); | ||
} | ||
}, | ||
|
||
close(peer, event) { | ||
console.log("[ws] close", peer, event); | ||
}, | ||
|
||
error(peer, error) { | ||
console.log("[ws] error", peer, error); | ||
}, | ||
|
||
// ... platform hooks such as bun:drain ... | ||
}); | ||
``` | ||
|
||
> [!NOTE] | ||
> 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# WebSocket Peer | ||
|
||
Peer object allows easily interacting with connected clients. | ||
|
||
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. | ||
|
||
> [!TIP] | ||
> 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! | ||
## API Reference | ||
|
||
### `peer.send(message, compress)` | ||
|
||
Send a message to the connected client | ||
|
||
### `peer.id` | ||
|
||
The peer address or unique id (might be `undefined`) | ||
|
||
### `peer.readyState` | ||
|
||
Client connection status (might be `undefined`) | ||
|
||
(read more in [MDN](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState)) | ||
|
||
### `peer.ctx` | ||
|
||
`peer.ctx` is an object that holds adapter specific context. It is scoped with `peer.ctx.[name]`. | ||
|
||
> [!NOTE] | ||
> This is an advanced namespace and API changes might happen, so don't rely on it as much aspossible! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# WebSocket Message | ||
|
||
On `message` hook, you receive a message object containing an incoming message from the client. | ||
|
||
> [!TIP] | ||
> You can safely log `message` object to the console using `console.log` it will be automatically stringified! | ||
## API Reference | ||
|
||
### `message.text()` | ||
|
||
Get stringified text version of the message | ||
|
||
### `message.rawData` | ||
|
||
Raw message data | ||
|
||
### `message.isBinary` | ||
|
||
Indicates if the message is binary (might be `undefined`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# WebSocket Client | ||
|
||
Universal WebSocket API to connect to WebSocket servers from clients. | ||
|
||
CrossWS exposes [`WebSocket`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) in order to connect to the server. | ||
|
||
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. | ||
|
||
```js | ||
import WebSocket from "crossws/websocket"; | ||
``` | ||
|
||
> [!NOTE] | ||
> Using export conditions, the correct version will be always used so you don't have to worry about picking the right build! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Runtime Adapters | ||
|
||
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. | ||
|
||
See Adapters section to learn more about all available built-in adapters | ||
|
||
## Integration with other runtimes | ||
|
||
You can define your custom adapters using `defineWebSocketAdapter` wrapper. | ||
|
||
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Bun | ||
|
||
Integrate CrossWS with Bun. | ||
|
||
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. | ||
|
||
```ts | ||
import wsAdapter from "./dist/adapters/bun"; | ||
|
||
const { websocket } = wsAdapter({ message: console.log }); | ||
|
||
Bun.serve({ | ||
port: 3000, | ||
websocket, | ||
fetch(req, server) { | ||
if (server.upgrade(req)) { | ||
return; | ||
} | ||
return new Response( | ||
`<script>new WebSocket("ws://localhost:3000").addEventListener('open', (e) => e.target.send("Hello from client!"));</script>`, | ||
{ headers: { "content-type": "text/html" } }, | ||
); | ||
}, | ||
}); | ||
``` | ||
|
||
## Adapter Hooks | ||
|
||
- `bun:message (peer, ws, message)` | ||
- `bun:open (peer, ws)` | ||
- `bun:close (peer, ws)` | ||
- `bun:drain (peer)` | ||
- `bun:error (peer, ws, error)` | ||
- `bun:ping (peer, ws, data)` | ||
- `bun:pong (peer, ws, data)` | ||
|
||
## Learn More | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Cloudflare | ||
|
||
Integrate CrossWS with Cloudflare Workers. | ||
|
||
To integrate CrossWS with your Cloudflare Workers, you need to check for the `upgrade` header. | ||
|
||
```ts | ||
import wsAdapter from "crossws/adapters/cloudflare"; | ||
|
||
const { handleUpgrade } = wsAdapter({ message: console.log }); | ||
|
||
export default { | ||
async fetch(request, env, context) { | ||
if (request.headers.get("upgrade") === "websocket") { | ||
return handleUpgrade(request, env, context); | ||
} | ||
return new Response( | ||
`<script>new WebSocket("ws://localhost:3000").addEventListener("open", (e) => e.target.send("Hello from client!"));</script>`, | ||
{ headers: { "content-type": "text/html" } }, | ||
); | ||
}, | ||
}; | ||
``` | ||
|
||
## Adapter Hooks | ||
|
||
- `cloudflare:accept (peer)` | ||
- `cloudflare:message (peer, event)` | ||
- `cloudflare:error (peer, event)` | ||
- `cloudflare:close (peer, event)` | ||
|
||
## Learn More | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Deno | ||
|
||
Integrate CrossWS with Deno. | ||
|
||
To integrate CrossWS with your Deno server, you need to check for the `upgrade` header and then call `handleUpgrade` method from the adapter passing the incoming request object. The returned value is the server upgrade response. | ||
|
||
```ts | ||
import wsAdapter from "crossws/adapters/deno"; | ||
|
||
const { handleUpgrade } = wsAdapter({ message: console.log }); | ||
|
||
Deno.serve({ port: 3000 }, (request) => { | ||
if (request.headers.get("upgrade") === "websocket") { | ||
return handleUpgrade(request); | ||
} | ||
return new Response( | ||
`<script>new WebSocket("ws://localhost:3000").addEventListener("open", (e) => e.target.send("Hello from client!"));</script>`, | ||
{ headers: { "content-type": "text/html" } }, | ||
); | ||
}); | ||
``` | ||
|
||
## Adapter Hooks | ||
|
||
- `deno:open (peer)` | ||
- `deno:message (peer, event)` | ||
- `deno:close (peer)` | ||
- `deno:error (peer, error)` | ||
|
||
## Learn More | ||
|
||
See [`playground/deno.ts`](./playground/deno.ts) for demo and [`src/adapters/deno.ts`](./src/adapters/deno.ts) for implementation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Node.js (uWebSockets) | ||
|
||
Integrate CrossWS with Node.js using uWebSockets.js. | ||
|
||
Instead of [using `ws`](/adapters/node-ws) you can use [uWebSockets.js](https://github.com/uNetworking/uWebSockets.js) for Node.js servers. | ||
|
||
```ts | ||
import { App } from "uWebSockets.js"; | ||
import wsAdapter from "crossws/adapters/node-uws"; | ||
|
||
const { websocket } = wsAdapter({ message: console.log }); | ||
|
||
const server = App().ws("/*", websocket); | ||
|
||
server.get("/*", (res, req) => { | ||
res.writeStatus("200 OK").writeHeader("Content-Type", "text/html"); | ||
res.end( | ||
`<script>new WebSocket("ws://localhost:3000").addEventListener('open', (e) => e.target.send("Hello from client!"));</script>`, | ||
); | ||
}); | ||
|
||
server.listen(3001, () => { | ||
console.log("Listening to port 3001"); | ||
}); | ||
``` | ||
|
||
## Adapter Hooks | ||
|
||
- `uws:open (ws)` | ||
- `uws:message (ws, message, isBinary)` | ||
- `uws:close (ws, code, message)` | ||
- `uws:ping (ws, message)` | ||
- `uws:pong (ws, message)` | ||
- `uws:drain (ws)` | ||
- `uws:upgrade (res, req, context)` | ||
- `uws:subscription (ws, topic, newCount, oldCount)` | ||
|
||
## Learn More | ||
|
||
See [`playground/node-uws.ts`](https://github.com/unjs/crossws/tree/main/playground/node-uws.ts) for demo and [`src/adapters/node-uws.ts`](https://github.com/unjs/crossws/tree/main/src/adapters/node-uws.ts) for implementation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Node.js (ws) | ||
|
||
Integrate CrossWS with Node.js using ws. | ||
|
||
To integrate CrossWS with your Node.js HTTP server, you need to connect the `upgrade` event to the `handleUpgrade` method returned from the adapter. CrossWS uses a prebundled version of [ws](https://github.com/websockets/ws). | ||
|
||
```ts | ||
import { createServer } from "node:http"; | ||
import wsAdapter from "crossws/adapters/node"; | ||
|
||
const server = createServer((req, res) => { | ||
res.end( | ||
`<script>new WebSocket("ws://localhost:3000").addEventListener('open', (e) => e.target.send("Hello from client!"));</script>`, | ||
); | ||
}).listen(3000); | ||
|
||
const { handleUpgrade } = wsAdapter({ message: console.log }); | ||
server.on("upgrade", handleUpgrade); | ||
``` | ||
|
||
## Adapter Hooks | ||
|
||
- `node:open (peer)` | ||
- `node:message (peer, data, isBinary)` | ||
- `node:close (peer, code, reason)` | ||
- `node:error (peer, error)` | ||
- `node:ping (peer)` | ||
- `node:pong (peer)` | ||
- `node:unexpected-response (peer, req, res)` | ||
- `node:upgrade (peer, req)` | ||
|
||
## Learn More | ||
|
||
See [`playground/node.ts`](https://github.com/unjs/crossws/tree/main/playground/node.ts) for demo and [`src/adapters/node.ts`](https://github.com/unjs/crossws/tree/main/src/adapters/node.ts) for implementation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
title: "CrossWS" | ||
description: "Cross-platform WebSocket Servers for Node.js, Deno, Bun and Cloudflare Workers." | ||
navigation: false | ||
hero: | ||
title: "[CrossWS]{.text-primary} :br [Cross-platform WebSockets]{.text-4xl}" | ||
description: "One code for Node.js, Deno, Bun and Cloudflare Workers" | ||
orientation: horizontal | ||
links: | ||
- label: "Get Started" | ||
icon: "i-heroicons-rocket-launch" | ||
to: "/getting-started" | ||
size: lg | ||
- label: "Contribute on GitHub" | ||
icon: "i-simple-icons-github" | ||
color: "white" | ||
to: "https://github.com/unjs/crossws" | ||
target: "_blank" | ||
size: lg | ||
# code: | ||
features: | ||
title: "WebSockets, supercharged." | ||
links: | ||
- label: "Get started" | ||
icon: "i-heroicons-rocket-launch" | ||
trailingIcon: "i-heroicons-arrow-right-20-solid" | ||
color: "gray" | ||
to: "/getting-started" | ||
size: lg | ||
items: | ||
- title: "Runtime-Agnostic" | ||
description: "Seamlessly integrates with Bun, Deno, Cloudflare Workrs and Node.js (ws or uWebSockets.js)" | ||
icon: "" | ||
- title: "Made for Performance" | ||
description: "High-performance server hooks, avoiding heavy per-connection events API" | ||
icon: "" | ||
- title: "Lightweight" | ||
description: "Zero Dependency with bundled ws for Node.js support. Extremely lightweight and tree-shakable packaging with ESM and CJS support." | ||
icon: "" | ||
- title: "Developer-friendly" | ||
description: "Typed Hooks API and human friednly logging support." | ||
icon: "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { defineDocsConfig } from "unjs-docs/config"; | ||
|
||
export default defineDocsConfig({ | ||
name: "CrossWS", | ||
description: | ||
"Cross-platform WebSocket Servers for Node.js, Deno, Bun and Cloudflare Workers.", | ||
github: "unjs/crossws", | ||
themeColor: "#f7932a", | ||
}); |
Oops, something went wrong.