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

websocket-relay

v0.3.3

Published

Easily set up communication channels between browser clients

Downloads

6

Readme

websocket-relay

Easily set up communication channels between browser clients

Great for creating WebRTC signaling servers

On the server:

var RelayServer = require('websocket-relay');
var relayServer = new RelayServer({ port: 4000 });

var client1 = relayServer.registerClient();
var client2 = relayServer.registerClient();

console.log(client1.id, client1.token); // => 0 11ac27cd0c17bd1c7ba9aa4285979aea
console.log(client2.id, client2.token); // => 1 7c2e2bc62cb06d7b90651cd4a2f369fb

relayServer.registerRelayChannel(client1.id, client2.id);

In the first browser client:

<script type="text/javascript" src="websocket_relay.min.js"></script>
<script type="text/javascript">
var relay = new WebSocketRelay('localhost:4000', {
  clientId: 0,
  token: '11ac27cd0c17bd1c7ba9aa4285979aea',
});
var channel = relay.createChannel(1);
</script>

In the second browser client:

<script type="text/javascript" src="websocket_relay.min.js"></script>
<script type="text/javascript">
var relay = new WebSocketRelay('localhost:4000', {
  clientId: 1,
  token: '7c2e2bc62cb06d7b90651cd4a2f369fb'
});
var channel = relay.createChannel(0);
</script>

Now you can freely send and receive data through the channel to the other browser client:

<script type="text/javascript">
channel.send('hello world');
channel.on('message', function(message) {
  console.log(message);
});
</script>

Server

Class: RelayServer

This class represents a WebSocket relay server. It is an EventEmitter.

new RelayServer(options, [callback])

  • options Object
    • port Number
    • authorizeAllChannels Boolean
  • callback Function

Construct a new server object.

options.authorizeAllChannels

Whether to allow relay channels between clients without registration. Default false.

server.close([callback])

Close the server and terminate all clients, calls callback when done with an error if one occured. Must be called after the RelayServer constructor calls its callback.

Event: 'error'

function (error) { }

If the underlying server emits an error, it will be forwarded here. If there is no listener, it will throw the error.

Client

Class: WebSocketRelay

This class represents a connection to a WebSocket relay server. It is an EventEmitter.

new WebSocketRelay(address, authentication, [callback])

  • address String
  • authentication Object
    • clientId Number
    • token String
  • callback Function

address is the host and port of the relay server. When it connects with the relay server callback is called.

authentication.clientId

ID of the client returned from registration on the server.

authentication.token

Client's authentication token.

relay.createChannel(targetClientId)

  • targetClientId Number

Returns an instance of RelayChannel that represents a bidirectional channel with the target client.

Class: RelayChannel

Represents a bidirectional channel with a target client. It is an EventEmitter.

channel.send(message)

  • message String

Sends the message to other client. If the other client has not connected the relay server yet, the message will be queued. Only string messages are supported.

channel.emitQueuedMessages()

Emits each queued message (the messages were received before the channel was created).

Event: 'message'

function (message) { }

The client received a relay message

Event: 'open'

function () { }

The client formed a connection with the relay server (channel.send() can be called).

Testing

npm test

Try upgrading node if you receive syntax errors.

Compiling Client Code

npm compile