npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ws-nats

v2.2.1

Published

A browser websocket client library for NATS

Downloads

21

Readme

ws-nats

npm License MIT

A browser websocket client library for NATS

Install

npm install --save ws-nats

Usage

<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
  <script src="ws-nats.umd.js"></script>
  <script>
    var nats = NATS.connect({ url: 'ws://0.0.0.0:8080', json: true });

    nats.publish('foo', { message: 'Hello World!' });
  </script>
</body>
</html>

Basic Example

// var NATS = window.NATS;
var NATS = require('ws-nats');

var nats = NATS.connect({ url: 'ws://0.0.0.0:8080', json: true });

// Simple Publisher
nats.publish('foo', { message: 'Hello World!' });

// Simple Subscriber
nats.subscribe('foo', function(msg) {
  console.log('Received a message: ' + msg);
});

// "*" matches any token, at any level of the subject.
nats.subscribe('foo.*.baz', function(msg, reply, subject) {
  console.log('Msg received on [' + subject + '] : ' + msg);
});

nats.subscribe('foo.bar.*', function(msg, reply, subject) {
  console.log('Msg received on [' + subject + '] : ' + msg);
});

// Using Wildcard Subscriptions

// ">" matches any length of the tail of a subject, and can only be
// the last token E.g. 'foo.>' will match 'foo.bar', 'foo.bar.baz',
// 'foo.foo.bar.bax.22'
nats.subscribe('foo.>', function(msg, reply, subject) {
  console.log('Msg received on [' + subject + '] : ' + msg);
});


// Unsubscribing
var sid = nats.subscribe('foo', function(msg) {});
nats.unsubscribe(sid);

// Request Streams
var sid = nats.request('request', function(response) {
  console.log('Got a response in msg stream: ' + response);
});

// Request with Auto-Unsubscribe. Will unsubscribe after
// the first response is received via {'max':1}
nats.request('help', {}, {'max':1}, function(response) {
  console.log('Got a response for help: ' + response);
});

// Request for single response with timeout.
nats.requestOne('help', {}, {}, 1000, function(response) {
  // `NATS` is the library.
  if(response instanceof NATS.NatsError && response.code === NATS.REQ_TIMEOUT) {
    console.log('Request for help timed out.');
    return;
  }
  console.log('Got a response for help: ' + response);
});

// Replies
nats.subscribe('help', function(request, replyTo) {
  nats.publish(replyTo, 'I can help!');
});

This library is compatible with all the API methods in node-nats

Testing

To test ws-nats, you need to connect to a NATS server using a Websocket-to-TCP relay such as nats-relay or ws-tcp-relay.

You can use Docker to run the gnatsd server and the Websockets to TCP relay:

# launch the gnatsd server
docker run -it --name=nats --rm -d -p 4222:4222 -p 8222:8222 nats -D -m 8222

# launch the relay (assumes we are using Linux!)
docker run -it --name=relay --rm -d -p 8080:8080 aaguilar/nats-relay -p 8080 $(hostname -i):4222

# then configure ws-nats to connect to the relay
var nats = NATS.connect({ url: 'ws://0.0.0.0:8080', json: true });

Browser support

Tested in the following browsers versions:

  • Google Chrome 53+
  • Firefox 37+
  • Internet Explorer 11
  • Microsoft Edge 12+
  • Safari 9+
  • Mobile Safari 11+
  • Opera 46+

Limitations

  • TLS connections to NATS server are not supported because we are using Websocket as transport
  • The nkeys public-key signature system has been disabled from the codebase to reduce bundle size (we plan to port this at a later stage)
  • The size of the library bundle has increased because we are using sockjs-client, we will remove this dependency in future releases.

Acknowledgements

This library is heavily inspired by websocket-nats and re-uses the sames API methods from the original node-nats library.