nats.ws is a robust JavaScript and TypeScript client library designed for interacting with the NATS messaging system over WebSocket connections. It supports a variety of environments including modern web browsers, Deno, and Node.js (requiring a WebSocket shim for Node.js). The library is currently at version 1.30.3 and maintains a frequent release cadence, often aligning with updates to its underlying core client logic (NBC, which is derived from `nats.deno`). A key differentiating factor is its specialized focus on WebSocket connectivity and its tight integration with the NATS ecosystem. Users are advised that nats.ws is now considered an integrated component within the larger nats.js monorepo, and future development and major feature enhancements are primarily focused there. Comprehensive migration documentation is available for transitioning to nats.js.
npm install nats.wsVerified import paths — ran on the pinned version, not inferred.
Demonstrates connecting to a NATS server via WebSocket, subscribing to a subject, responding to requests, publishing messages, and gracefully closing the connection.
Review the NATS.deno v1.29.2 release notes for specific instructions on migrating large object store data or adjusting your application logic.
Ensure your `nats-server` is updated to version v2.10.26 or newer if utilizing JetStream features, or review the `nats.deno` v1.29.3 release notes for specific impacts and necessary client-side adjustments.
Migrate to the `nats.js` package. Refer to the `migration.md` document within the `nats.js` GitHub repository for detailed migration guidance.
Always specify the full protocol in server URLs within `ConnectionOptions`, for example: `{ servers: ['ws://localhost:9222', 'wss://secure.nats.io'] }`.For heterogeneous or proxied environments, configure your connection with `connect({ servers: [...], ignoreServerUpdates: true })` and explicitly list all valid server URLs.Update your import statements to `import { connect } from 'nats.ws';` for versions 1.6.0 and higher. If targeting older versions, use the specific path.Install a WebSocket library like `websocket` (`npm install websocket`) and shim it globally before importing `nats.ws`: `globalThis.WebSocket = require("websocket").w3cwebsocket;`.Verify the NATS server URL and port are correct (including `ws://` or `wss://` protocol). Ensure the NATS server is running and accessible from the client. Check for any network or firewall rules that might be blocking the connection.
For `nats.ws` v1.6.0 and newer, change the import statement to `import { connect } from 'nats.ws';`. If the issue persists with bundlers, ensure your bundler is correctly configured to handle ESM exports and resolve the main entry point (e.g., by ensuring `module` field in `package.json` is respected).No dependency data recorded yet.