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

@web4/bitswarm-web

v1.0.1

Published

Implementation of the bitswarm API for use in web browsers

Downloads

12

Readme

bitswarm-web

Implementation of the bitswarm API for use in web browsers

Using in an application

npm i -s @web4/bitswarm-web
// Based on example in bitswarm repo
// Try running the regular bitswarm demo with node
const bitswarm = require('@web4/bitswarm-web')
const crypto = require('crypto')

const swarm = bitswarm({
  // Specify a server list of BitswarmServer instances
  bootstrap: ['ws://yourbitswarmserver.com'],
  // You can also specify proxy and signal servers separated
  wsProxy: [
    'ws://proxy1.com',
    'ws://proxy2.com'
  ],
  webrtcBootstrap: [
    'ws://signal1.com',
    'ws://signal2.com'
  ],
  // The configuration passed to the SimplePeer constructor
  // See https://github.com/feross/simple-peer#peer--new-peeropts
  // for more options
  simplePeer: {
    // The configuration passed to the RTCPeerConnection constructor, for more details see
    // https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/RTCPeerConnection#RTCConfiguration_dictionary
    config: {
      // List of STUN and TURN servers to connect
      // Without the connection is limited to local peers
      iceServers: require('./ice-servers.json')
    }
  },
  // Maximum number of peers (optional)
  // Used in both webrtc (default 5) and ws proxy config (default 24)
  maxPeers: 10,
  // Websocket reconnect delay in milliseconds (optional) (default 1000)
  wsReconnectDelay: 5000
})

// look for peers listed under this topic
const topic = crypto.createHash('sha256')
  .update('my-bitswarm-topic')
  .digest()

swarm.join(topic)

swarm.on('connection', (socket, details) => {
  console.log('new connection!', details)

  // you can now use the socket as a stream, eg:
  // socket.pipe(unichain.replicate()).pipe(socket)
})

swarm.on('disconnection', (socket, details) => {
  console.log(details.peer.host, 'disconnected!')
  console.log('now we have', swarm.peers.length, 'peers!')
})

Build it with Browserify to get it running on the web.

You could also compile an existing codebase relying on bitswarm to run on the web by adding a browser field set to {"bitswarm": "bitswarm-web"} to have Browserify alias it when compiling dependencies.

Setting up a proxy server

BitswarmServer provides two services:

  • BitswarmProxyWS: to proxy bitswarm connections over websockets. Path: ws://yourserver/proxy
  • SignalServer: for P2P WebRTC signaling connections. Path: ws://yourserver/signal

Running a BitswarmServer will allows you to use both services in one single process.

npm i -g @web4/bitswarm-web

# Run it! Default port is 4977 (HYPR on a phone pad)
bitswarm-web

# Run it with a custom port
bitswarm-web --port 42069

Running as a Linux service with SystemD

sudo cat << EOF > /etc/systemd/system/bitswarm-web.service
[Unit]
Description=Bitswarm proxy server which webpages can connect to.

[Service]
Type=simple
# Check that bitswarm-web is present at this location
# If it's not, replace the path with its location
# You can get the location with 'whereis bitswarm-web'
# Optionally add a --port parameter if you don't want 4977
ExecStart=/usr/local/bin/bitswarm-web
Restart=always

[Install]
WantedBy=multi-user.target
EOF

sudo chmod 644 /etc/systemd/system/bitswarm-web.service

sudo systemctl daemon-reload
sudo systemctl enable bitswarm-web
sudo systemctl start bitswarm-web

sudo systemctl status bitswarm-web