reconnex v1.0.9
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 reconnexUsage
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
waitTwitchWSConnectedWaits until the WebSocket connection is open.await reconnex.waitTwitchWSConnected()disconnectDisconnects the WebSocket connection.reconnex.disconnect('Optional reason');openOpens the WebSocket connection if not already opened.
reconnex.open()sendSends text or binary data over the WebSocket connection.reconnex.send('Sample message');jsonSends JSON data over the WebSocket connection.reconnex.json({ key: 'value' });sendJSONBinaryTransforms JSON data into binary and sends it over the WebSocket connection.reconnex.sendJSONBinary({ key: 'value' });sendOnConnectandremoveSendOnConnectAdds and removes strings to be sent on connection.reconnex.sendOnConnect('Authentication'); reconnex.removeSendOnConnect('Authentication');isConnectedChecks 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