reconnex
v1.0.9
Published
A simple Node JS library for connecting to websocket, with auto reconnect and utilities.
Downloads
188
Maintainers
Readme
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
andremoveSendOnConnect
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