Détail du package

crossws

h3js7.4mMIT0.4.1

Cross-platform WebSocket Servers for Node.js, Deno, Bun and Cloudflare Workers

readme

crossws

npm version npm downloads

Elegant, typed, and simple toolkit to implement cross-platform WebSocket servers.

👉 📖 documentation

Features

🧩 Seamlessly integrates with Bun, Cloudflare Workers, Deno and Node.js and any compatible web framework.

✅ Prebundled with ws for Node.js support with alternative/much faster uWebSockets adapter.

📦 Extremely lightweight and tree-shakable conditional ESM exports.

🚀 High-performance and simple hooks API, without per-connection callback creation.

🌟 Typed hooks API and developer-friendly object inspection.

[^1]: crossws supports Node.js via npm:ws (prebundled) or uWebSockets.js.

Contribution

<summary>Local development</summary> - Clone this repository - Install the latest LTS version of Node.js - Enable Corepack using corepack enable - Install dependencies using pnpm install - Run examples using pnpm play: scripts

License

Published under the MIT license. Made by @pi0 and community 💛


🤖 auto updated with automd

changelog

Changelog

v0.4.1

compare changes

🩹 Fixes

  • node server: Properly pass request to NodeRequest (28e5d64)

🏡 Chore

  • playground: Add start command (7ab898c)

❤️ Contributors

  • Pooya Parsa (@pi0)

v0.4.0

compare changes

🚀 Enhancements

  • Stub full request interface (#156)
  • Universal server for deno, node and bun using srvx (experimental) (#158)
  • Create PeerContext interface for type augmentation (#159)
  • ⚠️ Namespaced pub/sub peers (#162)
  • ⚠️ Support returning context from upgrade hook (#163)
  • cloudflare: Support global publish via rpc (#166)
  • Add cloudflare and default (sse) server entries (#167)

🩹 Fixes

  • ⚠️ Do not automatically accept first sec-webSocket-protocol (#142)

💅 Refactors

  • Remove uncrypto dependency (#153)
  • ⚠️ Always pass Request as first param to resolve (#160)
  • Simplify inspect values (aa49668)
  • Throw error when running deno, bun and node adapters in an incompatible environment (b5fcf2a)
  • Narrow down upgrade return type (d843cd0)
  • ⚠️ Always terminate upgrade if Response is returned (#164)
  • ⚠️ Merge cloudflare and cloudflare-durable adapters (#165)
  • cloudflare: Show warning when pub/sub is not supported (#144)

📖 Documentation

  • Change to h3js from unjs (#155)
  • Add docs for augmenting PeerContext type (#161)
  • Prepare for v0.4 (#168)

📦 Build

  • Simplify and fix exports (0d2ceb0)
  • Remove extra .d.ts files (1f389d6)

🏡 Chore

⚠️ Breaking Changes

  • ⚠️ Namespaced pub/sub peers (#162)
  • ⚠️ Support returning context from upgrade hook (#163)
  • ⚠️ Do not automatically accept first sec-webSocket-protocol (#142)
  • ⚠️ Always pass Request as first param to resolve (#160)
  • ⚠️ Always terminate upgrade if Response is returned (#164)
  • ⚠️ Merge cloudflare and cloudflare-durable adapters (#165)

❤️ Contributors

v0.3.5

compare changes

🚀 Enhancements

  • node: Support closeAll with force flag (#147)

🩹 Fixes

  • node: Destroy socket on upgrade abort (#140)

📦 Build

  • Export AdapterInternal type (#149)

🌊 Types

  • Mark NodeAdapter.handleUpgrade as async (#136)

🏡 Chore

❤️ Contributors

v0.3.4

compare changes

🚀 Enhancements

  • cloudflare: Support resolveDurableStub (#130)

🩹 Fixes

  • Specify an explicit return type for uint8Array() (#128)
  • node, uws: Send data as blob only if it is not string (#124)
  • Global publish via first subscribed peer (#103)
  • bun: Pass code and reason to close hook (#132)
  • Define request.context as read only (#133)

💅 Refactors

  • Stricter type declarations (#129)

📖 Documentation

  • node: Check upgrade === "websocket" in example (#131)
  • Add dynamic example for changeable resolve (e5daf22)

🏡 Chore

❤️ Contributors

v0.3.3

compare changes

🚀 Enhancements

  • Allow throwing error with .response prop in upgrade (#113)

❤️ Contributors

v0.3.2

compare changes

🚀 Enhancements

  • Support throwing responses in upgrade hook (#91)
  • peer: Support context (#110)
  • Shared context between upgrade hook and peer (#111)

🩹 Fixes

  • types: peer.request always has .headers if defined (e915f8d)
  • types: Mark peer.request as always defined (8fbb59b)

📖 Documentation

  • Fix typo (#85)
  • Fix typo (#84)
  • Add destr tip for JSON parsing (#109)

🏡 Chore

❤️ Contributors

v0.3.1

compare changes

🩹 Fixes

  • types: AdapterOptions type (#80)

🏡 Chore

❤️ Contributors

v0.3.0

compare changes

🌟 What is new?

Better stability

Crossws 0.3.x includes an overhaul of refactors, stability improvements, and new features. A new codebase and testing matrix had been implemented (#55) to make sure all supported adapters and runtimes work as expected and are consistent with each other.

Refined Peer API

The peer object allows easy interaction with connected WebSocket clients from server route hooks (peer docs).

To improve Web standards compatibility, accessing upgrade URL and headers is now possible with peer.request.url and peer.request.headers (breaking change), and peer.addr is also renamed to peer.remoteAddress to improve readability (breaking change) and support is increased across providers. You can also use new lazy-generated and secure peer.id (UUID v4) for various purposes including temporary sessions or persistent state.

Two new methods are now supported to close connected peers using peer.close(code, reason) and peer.terminate(). With this new version, you can access a standard WebSocket interface using peer.websocket.

[!NOTE] Today many of the server runtimes don't provide a spec-compliant WebSocket API. Crossws uses an internal proxy to polyfill consistent access to extensions, protocol, and readyState. See compatibility table for more details.

Refined Message API

On message hook, you receive a message object containing data from the client (message docs).

Parsing incoming messages can be tricky across runtimes. Message object now has stable methods .text(), .json(), .uint8Array(), .arrayBuffer(), .blob() to safely read message as desired format. If you need, you can also access .rawData, .peer, .event (if available), and lazy generated secure UUID v4 .id

Authentication via upgrade hook

When you need to authenticate and validate WebSocket clients before they can upgrade, you can now easily use the upgrade hook to check incoming URLs and headers/cookies and return a Web Standard Response in case you need to abort the upgrade.

Pubsub with Deno and Cloudflare Durable Objects

One of the common use cases of WebSockets is pubsub. This release adds pub-sub support to Deno provider and also you can globally broadcast messages using ws.publish for advanced use cases.

Normally with cloudflare workers, it is not possible to connect multiple peers with each other. Cloudflare Durable Objects (available on paid plans) allows building collaborative editing tools, interactive chat, multiplayer games, and applications that need coordination among multiple clients.

Crossws provides a new composable method to easily integrate WebSocket handlers with Durable Objects. Hibernation is supported out of the box to reduce billing costs when connected clients are inactive. (durable object peer docs)

Changelog

🚀 Enhancements

  • ⚠️ Overhaul internal implementation (#55)
  • ⚠️ Overhaul peer and message interface (#70)
  • node, uws: Automatically detect binary message type (#53)
  • peer: Add peer.close() and peer.terminate() support (#36)
  • Cloudflare durable objects support (#54) (docs)
  • deno: Support pub/sub (#58)
  • Universal access to all peers (#60)
  • Global publish using ws.publish (#61)
  • Experimental SSE-based adapter to support websocket in limited runtimes (#62, #66, #68) (docs
  • peer: Use secure lazy random UUID v4 (#64)

🩹 Fixes

  • Should not serailize binary messages (#39)
  • cloudflare-durable: Restore peer url and id after hibernation (#71)

💅 Refactors

  • ⚠️ Move peer.ctx to peer._internal (#59)
  • ⚠️ Remove adapter hooks (#72)
  • Rename internal crossws to hooks (bb4c917)
  • Better internal organization (2744f21)

📖 Documentation

#22, 76fc105, 7dacb00, #46, #45, #44, a96dca3, 898ab49, 2e49cc3

📦 Build

  • Remove optional uWebSockets.js dependency (#52, b23b76d)
  • ⚠️ Esm-only build (#63)

✅ Tests

  • Add adapter tests (#56)
  • cloudflare: Use random port for wrangler inspector (a46265c)
  • Run tests with web standard WebSocket and EventSource (#67)

❤️ Contributors

v0.2.4

compare changes

🚀 Enhancements

  • Auto generated peer id (a3b61f5)
  • Basic pubsub support for node (4bd61ca)

💅 Refactors

  • Improve peer inspect message (9f7e1f0)

📖 Documentation

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v0.2.3

compare changes

🩹 Fixes

  • node: Respect x-forwarded for client id (3f8bd0c)

❤️ Contributors

  • Pooya Parsa (@pi0)

v0.2.2

compare changes

🩹 Fixes

🏡 Chore

  • example: Handle secure origins (7f8639f)

❤️ Contributors

  • Pooya Parsa (@pi0)

v0.2.1

compare changes

🩹 Fixes

  • $callHook should check hook existence (40082ba)

📖 Documentation

❤️ Contributors

  • Pooya Parsa (@pi0)

v0.2.0

compare changes

💅 Refactors

  • ⚠️ Improve types and api (2ebacd3)

🏡 Chore

⚠️ Breaking Changes

  • ⚠️ Improve types and api (2ebacd3)

❤️ Contributors

  • Pooya Parsa (@pi0)

v0.1.3

compare changes

🏡 Chore

  • Add build script to release (6681afa)

❤️ Contributors

  • Pooya Parsa (@pi0)

v0.1.2

compare changes

🚀 Enhancements

  • Support `uWebSockets.js (b1de991)
  • Allow access to peer url and headers (b67bef0)
  • Dynamic resolver (cb6721c)
  • Support upgrade hook to set headers (91edb54)
  • Pub/sub support for bun and uws (a486f45)

💅 Refactors

📖 Documentation

  • Add link to play online (ed41540)
  • Migrate to unjs-docs structure v2 (#13)
  • Update bun (0c717d8)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v0.1.1

compare changes

💅 Refactors

  • Import bun types from @types/bun (ba40b53)

📦 Build

  • Expose default export for compatibility types (9934fb5)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v0.0.1

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)