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

socketto

v2.0.1

Published

Tiny wrapper for WebSocket Web API

Downloads

12

Readme

Installation

Install with npm:

npm i socketto

Install with yarn:

yarn add socketto

Usage

You can create a WebSocket connection based on the example below:

import Socketto from 'socketto'

const ws = new Socketto('ws://localhost:8080',
  { // these events are optional
    onOpen: () => console.log('OPEN'),
    onReconnect: () => console.log('RECONNECT'),
    onMessage: (data) => { console.log(`RECEIVED MESSAGE ${data}`) },
    onRetry: () => { console.log('RETRY TO CONNECT') },
    onFailed: () => { console.log('FAILED TO CREATE CONNECTION') }
  },
  { // these options are optional
    waitToReconnect: 1000,
    maxReconnectAttempts: 4
  }
)

// open connection
ws.createConnection()

Events

You can add callbacks up to four event handler that WebSocket listens to:

onOpen()

This event will be triggered when WebSocket connection is opened

onReconnect()

This event will be triggered when WebSocket connection is estabilished successfully after retry

onMessage(data)

This event will be triggered when WebSocket receives message from server. Usually is used to render the message in UI. The parameter can be anything (e.g. string, object, etc.)

onRetry()

This event will be triggered everytime WebSocket try to reconnect

onFailed()

This event will be triggered when WebSocket failed to create a connection after retry a certain times

Options

waitToReconnect

How long WebSocket will wait before trying to reconnect. Default value is 3 seconds

maxReconnectAttempts

Maximum retry number allowed. Default value is 3

Reconnect

Socketto can reconnect by default using exponential backoff. It means that Socketto will increase the waiting time between retries after each retry failure. On default configuration, Socketto will try to reconnect 3 times, and wait for 3 seconds at the first retry. You can change the configuration when opening the connection. For example:

import Socketto from 'socketto'

const ws = new Socketto('ws://localhost:8080', {
  // event callbacks
}, {
  maxReconnectAttempts: 5,
  waitToReconnect: 5000
})

With this configuration, Socketto will try to reconnect 5 times maximum, and will wait for 5 seconds for the first retry.

API

.createConnection()

ws.createConnection()

This API will try to open a WebSocket connection based on constructor parameters

.closeConnection()

ws.closeConnection()

Close the WebSocket connection. By calling this API, WebSocket will immediately close its connection without retry

.send()

ws.send('send-dummy-message')

Send any data through WebSocket connection

.readyState

ws.readyState

Returns the current state of WebSocket connection based on MDN
|Value | State | Description | |------|------------|----------------------------------------------------------| | 0 | CONNECTING | Socket has been created. The connection is not yet open. | | 1 | OPEN | The connection is open and ready to communicate. | | 2 | CLOSING | The connection is in the process of closing. | | 3 | CLOSED | The connection is closed or couldn't be opened. |