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

ultrawave

v0.0.21

Published

shared state between peers over WebRTC data channels

Downloads

13

Readme

Ultrawave

Ultrawave is a library enabling shared state through peer to peer connections over WebRTC data channels. Ultrawave makes it easy to build things like real time collaborative editing, messaging, and games. It works great with, but is not tied to, React.

To build an app with ultrawave, you connect a group of peers and provide an initial chunk of JSON serializable data - peers are able to read and write to the shared data through cursors, with changes made by any peer showing up for everyone in the group. Data is eventually consistant between peers, even in the face of lost, duplicated, or misordered messages.

There are some repos with example code available, as well as an abbreviated example at the end of this readme:

Installation

Grab the library from npm: npm install ultrawave

Creating a new ultrawave

WebRTC allows direct communication between peers, but requires a server to form the initial connections. You can run a simple websocket server for peering by creating an instance of UltrawaveServer

const UltrawaveServer = require('ultrawave/server')
let server = new UltrawaveServer({port: 5000})

Then, in the browser, create an ultrawave and provide the url for your peering server.

peer = new Ultrawave('ws://localhost:5000')

Creating a peer group

Create a group by providing a group name, some initial data, and a callback to handle changes. Whenever the data is updated, locally or by a peer, your callback will be passed a root cursor which it can use to read or write data.

peer.create(group, data, (cursor) => {
  React.render(<Component cursor={cursor}>, el)
})

You can also join an existing group by name,

peer.join(group, (cursor) => {
  React.render(<Component cursor={cursor}>, el)
})

...use joinOrCreate if you don't care which one you do,

peer.joinOrCreate(group, data, (cursor) => {
  React.render(<Component cursor={cursor}>, el)
})

...or leave a group.

peer.leave(group)

Changes made by any peer are applied everywhere

Data in Ultrawave forms a tree structure where nodes are either arrays or objects. In messages between peers, changes are represented as the path from the root to the node to be modified, a method, and arguments. Available methods on objects are 'set', 'delete', and 'merge', and on arrays are 'set', 'delete', and 'splice' (and shortcuts based on splice: 'push', 'pop', 'shift' and 'unshift').

These changes are made through cursors - objects that wrap the path to a specific node, and allow reads and writes to that node and its subtree. Every peer connects to every other peer in its group, and sends any changes made through its cursors to each of its peers. For the full cursor api, see the subtree package.

Because messages can be lost or received out of order, vector clocks are used to detect missed messages and create an ordering for changes allowing the document state to be eventually consistant between peers.

Example

This is an example of a simple chat app built with Ultrawave and React in 20 lines. It creates a group with an empty array as initial data, then renders a react component when data changes. The component is passed a cursor as a prop, from which it reads data to render the messages. When the button is clicked, the component updates the data by pushing a new message onto the cursor causing it to be sent to each peer.

const Chat = React.createClass({
  render: function() {
    const messages = this.props.cursor
    return <div>
      {messages.get().map((message) => <p>{message}</p>)}
      <input ref='input'/>
      <button
        onClick={() => {
          const input = refs.input.getDOMNode()
          messages.push(input.value)
          input.value = ''
        }}
      >send</button>
    </div>
  }
})

peer.joinOrCreate('chatroom', [], (cursor) => {
  React.render(<Chat cursor={cursor}/>, document.body)
})

Considerations

Ultrawave is great if you want to easily build peer to peer apps, but here are a few cases in which it will not work well:

  • Right now only Chrome and Firefox support WebRTC peer to peer connections. Microsoft has announced planned support, but if you need your app to run in IE or Safari today, Ultrawave (and WebRTC in general) will not work for you.

  • For users behind firewalls, the peer to peer connections used by WebRTC may be blocked - this can be worked around by proxying traffic between peers through a TURN server, but if you end up needing to use a central server anyways, WebSockets are likely to be a better choice.

  • Ultrawave allows any peer to edit any part of the data tree and does not attempt to validate changes. If you are building an app where trust between peers is an issue (for example a game where you are worried about cheating), ultrawave is not a good choice. Ultrawave is built on a lower level messaging library peergroup, which might work better for you.

About

Ultrawave was originally written for the sinesaw web audio DAW project. It uses peergroup for messaging over WebRTC data channels, and subtree for immutable data modeling with cursors.