-
Notifications
You must be signed in to change notification settings - Fork 16
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
17 changed files
with
279 additions
and
249 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,28 @@ | ||
# yaml-language-server: $schema=https://unpkg.com/undocs/schema/config.json | ||
|
||
name: CrossWS | ||
shortDescription: runtime agnostic WebSockets | ||
description: Unified WebSocket API for Node.js, Deno, Bun and Cloudflare | ||
Workers. | ||
github: unjs/crossws | ||
landing: | ||
heroLinks: | ||
playOnline: | ||
label: Play Online | ||
icon: i-heroicons-play | ||
to: https://stackblitz.com/github/unjs/crossws/tree/main/examples/h3?file=app.ts | ||
features: | ||
- title: Runtime Agnostic | ||
description: | ||
Seamlessly integrates with Bun, Deno, Cloudflare Workrs and Node.js | ||
(ws or uWebSockets.js) | ||
- title: Made for Performance | ||
description: | ||
High-performance server hooks API designed to avoid creating callbacks | ||
per client but once. | ||
- title: Lightweight | ||
description: | ||
Zero Dependency with bundled ws for Node.js support. Extremely lightweight | ||
and tree-shakable packaging with ESM and CJS support. | ||
- title: Developer Friendly | ||
description: Typed Hooks API and human friednly logging support. |
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 |
---|---|---|
@@ -1,41 +1,64 @@ | ||
--- | ||
icon: ph:book-open-duotone | ||
--- | ||
|
||
# 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. | ||
> 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! | ||
|
||
## Play Online | ||
> [!IMPORTANT] | ||
> CrossWS API is still under development and can change. | ||
You can quickly try CrossWS online with [unjs/h3](https://h3.unjs.io) and [unjs/listhen](https://listhen.unjs.io): | ||
## Quick Start | ||
|
||
::callout{to="https://stackblitz.com/github/unjs/crossws/tree/main/examples/h3?file=app.ts"} | ||
Play online in Stackblitz | ||
:: | ||
> [!TIP] | ||
> You can try CrossWS with [online playground](https://stackblitz.com/github/unjs/crossws/tree/main/examples/h3?file=app.ts) or using [unjs/h3](https://h3.unjs.io) + [unjs/listhen](https://listhen.unjs.io) or alternatively integrate it with your own framework. | ||
A simple WebSocket implementation looks like this: | ||
|
||
```ts | ||
import { defineWebSocketHooks } from "crossws"; | ||
|
||
## Installing Package | ||
websocket = defineWebSocketHooks({ | ||
open(peer) { | ||
console.log("[ws] open", peer); | ||
}, | ||
|
||
You can install crossws from [npm](https://npmjs.com/crossws): | ||
message(peer, message) { | ||
console.log("[ws] message", peer, message); | ||
if (message.text().includes("ping")) { | ||
peer.send("pong"); | ||
} | ||
}, | ||
|
||
::code-group | ||
close(peer, event) { | ||
console.log("[ws] close", peer, event); | ||
}, | ||
|
||
```sh [auto] | ||
npx nypm i crossws | ||
``` | ||
error(peer, error) { | ||
console.log("[ws] error", peer, error); | ||
}, | ||
}); | ||
``` | ||
|
||
::read-more{to="/guide/api" title="API Usage"} | ||
See [API Usage](/guide/api) for more usage details. | ||
:: | ||
|
||
::read-more{to="/adapters" title="Adapters"} | ||
The universal handler can be easily integrated with different platforms and frameworks. See [Adapters](/adapters) for more details. | ||
:: | ||
|
||
```sh [npm] | ||
npm install crossws | ||
``` | ||
## Using Package | ||
|
||
```sh [Yarn] | ||
yarn add crossws | ||
``` | ||
You can install `crossws` from [npm](https://npmjs.com/crossws) in your project: | ||
|
||
```sh [pnpm] | ||
pnpm add crossws | ||
``` | ||
:pm-install{name="crossws"} | ||
|
||
```sh [bun] | ||
bun add crossws | ||
``` | ||
Alternatively you can import it from CDN: | ||
|
||
:: | ||
```js | ||
import { crossws } from "https://esm.sh/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,95 @@ | ||
--- | ||
icon: mynaui:api | ||
--- | ||
|
||
# API | ||
|
||
> 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. | ||
|
||
> [!IMPORTANT] | ||
> CrossWS API is still under development and can change. | ||
```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 ... | ||
}); | ||
``` | ||
|
||
> [!TIP] | ||
> 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. | ||
## Peer Object | ||
|
||
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 | ||
|
||
#### `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{to="https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState" title="readyState in MDN"} | ||
|
||
#### `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! | ||
## Message Object | ||
|
||
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 | ||
|
||
#### `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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
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
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
Oops, something went wrong.