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 🙏

© 2026 – Pkg Stats / Ryan Hefner

choo-websocket

v2.0.0

Published

Small wraper around WebSocket browser API, for choo apps

Readme

choo-websocket stability

npm version build status downloads js-standard-style

Small wrapper around WebSocket browser API, for choo apps

Usage

var choo = require('choo')
var html = require('choo/html')

var app = choo()
app.use(require('choo-websocket')())
app.route('/', mainView)
app.mount('body')

function mainView (state, emit) {
  return html`
    <body>
      <pre id="results"></pre>
      <p>Send something through sockets:</p>
      <input id="message" />
      <button onclick=${onclick}>Send</button>
    </body>
  `

  function onclick () {
    emit('ws:send', document.getElementById('message').value)
    document.getElementById('message').value = ''
  }
}

function live (state, emitter) {
  emitter.on('DOMContentLoaded', function () {
    emitter.on('ws:open', () => {
      console.log('Connection established')
    })
    emitter.on('ws:message', (data, event) => {
      var msgElement = document.getElementById('results')
      msgElement.textContent = msgElement.textContent + data + '\n'
    })
  })
}

Events

ws:error | ws.events.ERROR

Emitted if the WebSocket constructor or any of its methods throws an exception.

ws:open | ws.events.OPEN

Emitted when the connection is established and the readyState property of the socket changes to OPEN. You should only listen to this method, since the constructor and the add-socket event silently open sockets for you. This event handler will get two arguments, the event object, and the id of the socket that got opened, you can use that id to directly access to the socket through state.sockets[id] or passing it to events like send and close. If you don't pass the id, it will asume the default socket (the one created when the plugin got registered).

ws:close | ws.events.CLOSE

Emitted when the connection is closed and the readyState property of the socket changes to CLOSED. When you listen to this, handler will take two arguments, the event object and the socket id. When you emit this, it will take three arguments the code for the close, the reason string and the id of the socket you want to close. If you don't pass the id, it will asume the default socket (the one created when the plugin got registered).

ws:send | ws.events.SEND

Emitted to send a message through the socket. Emit this event passing the data you want to send, and an optional id as reference of the socket that you want to send the message.

ws:message | ws.events.MESSAGE

Listen to this event to get messages from the socket. The handler of this event will get three arguments, the data sent, the whole evet and the id of the socket that got the message.

ws:add-socket | ws.events.ADD_SOCKET

Add a socket. Accept a route, an options object and an optional id, if you pass a route of an existing socket it will override it. If you don't pass any id, it will create a random string as id. This id is used to identify the WebSocket instance in the app state and to emit and listen for events only to specific instances. Keep in mind that this is agnostic to the server side implementation, so its up to you to handle different websockets from server. In the example folder, there is a simple implementation of rooms using the ws module. Some other libraries like socket.io has this concept built in.

API

plugin = ws([route], [opts])

The plugin accepts three optional parameters. You can pass the route for the web socket, which defaults to window.location.host. Notice that you don't need to specify the ws protocol. Also you can pass some options as a second argument.

  • secure: Boolean. Set to true if you are in a secure environment. If you mix environment, it will throw on creation of the socket.
  • protocols: Array or String. Use it to specify sub protocols for the socket.

If the object is correctly created, then you have a sockets object where every propertie is a key to the WebSocket instance that you added.

See Also

  • choo-sse - Small wrapper around server-sent event browser API, for choo apps.

License

MIT