About
A simple Node JS library for connecting to websocket, with auto reconnect and utilities
Explore the docs »
Report Bug
.
Request Feature
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
This library only uses ws to establish the connection
First install our library
- Reconnex
npm install reconnexImport the library
import { Reconnex } from 'reconnex'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()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.
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}`)
})waitForSocketConnectWaits until the WebSocket connection is open.
await reconnex.waitForSocketConnect()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();Distributed under the MIT License. See LICENSE for more information.
- ZackSB ZackSB - Built Reconnex