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

reconnex

v1.0.9

Published

A simple Node JS library for connecting to websocket, with auto reconnect and utilities.

Downloads

369

Readme

Contributors Issues License npm version

About The Project

This library was created to facilitate the use of websocket, focused more specifically on reconnection, with it it is possible to send parameters that can be used for authentication, and if the connection is closed, when the connection is resumed, it will resend the data for authentication again

Built With

This library only uses ws to establish the connection

Getting Started

Installation

First install our library

  • Reconnex
npm install reconnex

Usage

Import the library

import { Reconnex } from 'reconnex'

instantiate the class

const twitchChatWs = 'wss://irc-ws.chat.twitch.tv/'
const reconnex = new Reconnex({
  url: twitchChatWs,
  ping: {
    // Use this object when you want to automate ping
    data: 'PING', // Data sent with each ping
    interval: 4 * 60 * 1000, // Interval time between each sending
  },
  options: {}, // Ws connection options
  reconnect: {
    maxAttempts: -1, // Maximum attempts to reconnect, use -1 for infinite, default is 10
    connectTimeout: 10 * 1000, // Waiting time to try to reconnect. By default it is 5 seconds
  },
})

open the connection

reconnex.open()

Sending Authentication Payloads

const joinChannel = 'zvods'

const authenticationPayloads = ['CAP REQ :twitch.tv/tags twitch.tv/commands', 'PASS SCHMOOPIIE', 'NICK justinfan4194', 'USER justinfan4194 8 * :justinfan4194', `JOIN #${joinChannel}`]

authenticationPayloads.forEach((payload) => reconnex.sendOnConnect(payload))

sendOnConnect is perfect for fixed authentications and room entries; it automatically resends specified parameters upon reconnection, ensuring seamless continuity.

Event Handling

reconnex.on('send', (data) => {
  console.log(`Data sent: ${data}`)
})

reconnex.on('error', (err) => {
  if (err.code === 'ENOTFOUND') return console.log('No Internet Connection')
  console.error(err)
})

reconnex.on('open', (url) => {
  console.log(`Connected at ${url}`)
})

reconnex.on('close', (code, reason) => {
  console.log(`WebSocket disconnected with code ${code} ${reason}`)
})

reconnex.on('retry', (attempt, max) => {
  console.log(`Trying to reconnect ${attempt} of ${max}`)
})

reconnex.on('max_attempt', () => {
  console.log('Reconnect attempt limit reached')
})

reconnex.on('message', (message) => {
  // console.log(message) default websocket message
})

reconnex.on('text', (text) => {
  console.log(`Text Received: ${text}`)
})

Additional Functions

  • waitTwitchWSConnected Waits until the WebSocket connection is open.
 await reconnex.waitTwitchWSConnected()
  • disconnect Disconnects the WebSocket connection.
 reconnex.disconnect('Optional reason');
  • open Opens the WebSocket connection if not already opened.
  reconnex.open()
  • send Sends text or binary data over the WebSocket connection.
 reconnex.send('Sample message');
  • json Sends JSON data over the WebSocket connection.
 reconnex.json({ key: 'value' });
  • sendJSONBinary Transforms JSON data into binary and sends it over the WebSocket connection.
 reconnex.sendJSONBinary({ key: 'value' });
  • sendOnConnect and removeSendOnConnect Adds and removes strings to be sent on connection.
 reconnex.sendOnConnect('Authentication');
 reconnex.removeSendOnConnect('Authentication');
  • isConnected Checks if the WebSocket connection is open.
 const isConnected = reconnex.isConnected();

License

Distributed under the MIT License. See LICENSE for more information.

Authors

  • ZackSB - Master's degree in life - ZackSB - Built Reconnex

Acknowledgements