Skip to content

Latest commit

 

History

21 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


Logo

Reconnex

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

Explore the docs »

Report Bug . Request Feature

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'

Browser

The library is available on the window object as socket.

<script src="https://cdn.jsdelivr.net/npm/reconnex/dist/reconnex.browser-global.min.js"></script>
<script>
  const reconnex = new socket.Reconnex({
    url: "wss://irc-ws.chat.twitch.tv/"
  })
  reconnex.on('open', (url) => {
    console.log(`Connected at ${url}`)
  })
  reconnex.on('close', (code, reason) => {
    console.log(`Websocket disconnected with code ${code} ${reason}`)
  })
  reconnex.open()
</script>

instantiate the class

const reconnex = new Reconnex({
  url: 'wss://irc-ws.chat.twitch.tv/',
  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 (Twitch chat example)

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

  • waitForSocketConnect Waits until the WebSocket connection is open.
 await reconnex.waitForSocketConnect()
  • 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 ZackSB - Built Reconnex

Acknowledgements

Support:

buy me a coffe

Donate with livepix



About

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

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages