
Research
/Security News
9 Malicious NuGet Packages Deliver Time-Delayed Destructive Payloads
Socket researchers discovered nine malicious NuGet packages that use time-delayed payloads to crash applications and corrupt industrial control systems.
@iam4x/better-websocket
Advanced tools
A robust WebSocket wrapper with auto-reconnection, URL fallback, message queuing, and heartbeat functionality.
A robust WebSocket wrapper with auto-reconnection, URL fallback, message queuing, and heartbeat functionality.
npm i -S @iam4x/better-websocket
import { BetterWebSocket } from '@iam4x/better-websocket';
// Simple usage - drop-in replacement for WebSocket
const ws = new BetterWebSocket('ws://localhost:8080');
ws.onopen = () => console.log('Connected!');
ws.onmessage = (event) => console.log('Message:', event.data);
ws.onclose = () => console.log('Disconnected');
ws.onerror = (error) => console.log('Error:', error);
ws.send('Hello, WebSocket!');
const ws = new BetterWebSocket([
'ws://primary-server.com',
'ws://backup-server.com',
'ws://fallback-server.com'
], undefined, {
connectTimeout: 5000,
maxReconnectAttempts: 10,
reconnectBackoffFactor: 1.5
});
const ws = new BetterWebSocket('ws://localhost:8080', undefined, {
enableHeartbeat: true,
heartbeatInterval: 30000, // Send ping every 30 seconds
heartbeatTimeout: 5000, // Expect pong within 5 seconds
heartbeatMessage: 'ping' // Custom heartbeat message
});
const ws = new BetterWebSocket('ws://localhost:8080', undefined, {
maxQueueSize: 100
});
// Messages sent while disconnected are queued
ws.send('This will be queued if not connected');
ws.send('This too');
// Check queue status
console.log('Queued messages:', ws.getQueuedMessageCount());
// Clear queue if needed
ws.clearMessageQueue();
interface BetterWebSocketOptions {
connectTimeout?: number; // Connection timeout in ms (default: 10000)
maxReconnectAttempts?: number; // Max reconnection attempts (default: 5)
reconnectBackoffFactor?: number; // Backoff multiplier (default: 1.5)
heartbeatInterval?: number; // Heartbeat interval in ms (default: 30000)
heartbeatTimeout?: number; // Heartbeat response timeout in ms (default: 5000)
enableHeartbeat?: boolean; // Enable heartbeat functionality (default: false)
heartbeatMessage?: string; // Heartbeat message (default: 'ping')
maxQueueSize?: number; // Max queued messages (default: 100)
protocols?: string | string[]; // WebSocket protocols
}
new BetterWebSocket(
urls: string | string[],
protocols?: string | string[],
options?: BetterWebSocketOptions
)
All standard WebSocket methods are supported:
send(data) - Send data (queued if not connected)close(code?, reason?) - Close connectionreadyState, url, protocol, extensions, etc.onopen, onmessage, onclose, onerrordestroy() - Permanently destroy the connection (prevents reconnection)forceReconnect() - Force an immediate reconnectiongetQueuedMessageCount() - Get number of queued messagesclearMessageQueue() - Clear the message queuegetCurrentUrl() - Get the currently active URLgetReconnectAttempts() - Get current reconnection attempt countBetterWebSocket supports both event handlers and addEventListener:
// Event handlers
ws.onopen = (event) => { /* ... */ };
ws.onmessage = (event) => { /* ... */ };
ws.onclose = (event) => { /* ... */ };
ws.onerror = (event) => { /* ... */ };
// Event listeners
ws.addEventListener('open', (event) => { /* ... */ });
ws.addEventListener('message', (event) => { /* ... */ });
ws.addEventListener('close', (event) => { /* ... */ });
ws.addEventListener('error', (event) => { /* ... */ });
enum BetterWebSocketState {
CONNECTING = 0,
OPEN = 1,
CLOSING = 2,
CLOSED = 3
}
maxReconnectAttemptsmaxQueueSize)When enabled, BetterWebSocket will:
BetterWebSocket gracefully handles:
Run the comprehensive test suite:
bun test
The tests include integration tests with real WebSocket servers to ensure reliability.
# Install dependencies
bun install
# Run tests
bun test
# Build
bun run build
# Lint
bun run lint
MIT
FAQs
A robust WebSocket wrapper with auto-reconnection, URL fallback, message queuing, and heartbeat functionality.
The npm package @iam4x/better-websocket receives a total of 0 weekly downloads. As such, @iam4x/better-websocket popularity was classified as not popular.
We found that @iam4x/better-websocket demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Research
/Security News
Socket researchers discovered nine malicious NuGet packages that use time-delayed payloads to crash applications and corrupt industrial control systems.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.