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

mytosis-websocket

v0.1.1

Published

Mytosis WebSocket plugin

Downloads

4

Readme

Mytosis WebSocket

Send real-time updates over websockets

What it's for

Mytosis defines a standard interface for network plugins, but it doesn't ship with any out of the box. mytosis-websocket is one such adapter for websockets (I know, never would've guessed).

Here's what it does:

  • Runs uWebSockets on the server because it's wicked fast
  • Connects with reconnecting-websocket on the front end because it's tiny and reconnects automatically
  • Supports binary payloads (Blob, File, Node's Buffer, ArrayBuffer) so you can finally send those cat pictures in real time

Here's what it won't do:

  • Fall back to http polling. WebSockets have great support (IE10+ in web units). If you're going retro or just really love Flash, write your own plugin.
  • Manage your data for you. That's not how Mytosis works. You're in charge of your data. This plugin does nothing until you tell it to (in fact, these types of plugins don't even have a reference to your database). Take a look at the Router plugin type for more deets.

Installing

It's on npm as mytosis-websocket. You probably know the drill, but here's something you can copy & paste.

# Yarn good
yarn add mytosis-websocket

# npm less good
npm install --save mytosis-websocket

Using it

Note: there's this great websocket server on the internet that just shouts back whatever you send it. I use that URL in the examples: ws://html5rocks.websocket.org/echo

There are two parts, the client and the server. They work best together, but it's not necessary.

By default, it'll use the global WebSocket instance. Good for browsers...

import Socket from 'mytosis-websocket'
import database from 'mytosis'

const socket = new Socket('ws://html5rocks.websocket.org/echo')

const db = database({ network: socket })

Configuring the client

Need it in Node? Since there's no global WebSocket constructor, you'll need to pass your own. Two good libraries are uws and ws. This library depends on uws so if you don't really care, just pick that one.

import Socket from 'mytosis-websocket'
import WebSocket from 'uws'

const socket = new Socket('ws://html5rocks.websocket.org/echo', {
  WebSocket: WebSocket,
})

When a websocket disconnects, it'll automatically try to reconnect. All of the reconnection stuff is configurable.

const defaultOptions = {
  WebSocket: TheGlobalSocketConstructor,
  protocols: undefined, // It's the second param to `WebSocket`
  reconnectionDelayGrowFactor: 1.3,
  maxReconnectionDelay: 10000,
  minReconnectionDelay: 1500,
  connectionTimeout: 4000,
  maxRetries: Infinity,
  debug: false,
}

Note: Most of the above snippet is taken from the reconnecting-websocket readme.

Server

The server is mostly the same.

// Mind the appended "/server"
import Server from 'mytosis-websocket/server'
import database from 'mytosis'

const server = new Server({
  port: 8080,
})

const db = database({
  network: server,
})

Boom! You've got a WebSocket server. High five, my friend! :raised_hand_with_fingers_splayed:

Using the client and server simultaneously is a bit weird with Mytosis, you've gotta put it in two configs:

const db = database({
  network: new Server({ port: 8080 }),
}, {
  network: new Socket(url, config),
})

Setting options for the server

This library uses uws is basically a drop-in replacement for ws, so everything you can pass ws you can pass to mytosis-websocket/server. That's where the config.port option comes from. Here's a link to some docs.

Another common option is config.server, which allows you to pass your own http.Server instance. Rather handy for use with express.

import SocketServer from 'mytosis-websocket/server'
import { Server } from 'http'

const server = new Server()
const socketServer = new SocketServer({ server })

server.listen(8080)

Examples

Watching for new connections:

import Server from 'mytosis-websocket/server'

const server = new Server({ port: 8080 })

server.messages.forEach((message) => {
  console.log('New message:', message)
})

server.on('add', (connection) => {
  connection.send({ hey: `how's it going, ${connection.id}?` })
})

server.on('remove', (connection) => {
  console.log(`Sad, ${connection.id} left.`)
})

All Mytosis servers will work like this. It's part of the spec.

Here's how you'd listen for updates and send changes:

const server = new Server({ port: 8080 })

const db = database({
  network: server,
  router: (db, config) => {
    config.network.messages.forEach((message, sender) => {
      // If the other client is running the same router, you should see
      // something like `type: 'read', key: 'whatevs'`.
      // Everything you send is completely up to you.
      sender.send(someResponse)
    })

    return {
      pull (read) {
        read.network.send({ type: 'read', key: read.key })
      },

      push (write) {
        write.network.send({ type: 'write', update: write.update })
      },
    }
  }
})

There you go!

Support

Real-time updates are still a new feature of Mytosis. If you find a problem, please post an issue. It could influence the future of the plugin spec. Seriously, don't be shy.

Questions and comments are also good for GitHub issues.