Registry / messaging / nanosocket

nanosocket

JSON →
library1.1.0jsnpmunverified

A tiny WebSocket client (~1KB bundled) with built-in reconnection and exponential backoff, offering callback/promise/stream-based message handling via emitterify. Version 1.1.0 is the current stable release. It is a lightweight alternative to heavier WebSocket libraries, focusing on minimal overhead and reconnection handling. The library works in browser and Node.js environments, but requires emitterify for stream/callback functionality.

npm install nanosocket
INSTALL
IMPORT
SIG · NANOSOCKET
N
nanosocket
messagingjavascriptv1.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

nanosocket
import nanosocket from 'nanosocket'
const { nanosocket } = require('nanosocket')
Default export only; CJS require returns the default as module.exports.
send/on (via nanosocket)
import nanosocket from 'nanosocket' const socket = nanosocket('wss://example.com') socket.send('msg').then(...) socket.on('recv').map(d => ...)
const socket = nanosocket(); socket.on('message', ...) (no such method)
Use on('recv') for incoming messages, not 'message'. send() returns a promise.
default (type)
import type nanosocket from 'nanosocket' (if using with TypeScript)
No official type definitions; may need custom .d.ts if using TypeScript.

Creates a WebSocket connection, logs incoming messages, connection events, and sends a message.

import nanosocket from 'nanosocket' const socket = nanosocket('wss://echo.websocket.org') socket.on('recv').map(d => console.log('received:', d)) socket.on('connected').map(d => console.log('connected')) socket.on('disconnected').map(d => console.log('disconnected')) socket.send('Hello').then(() => console.log('sent')) setTimeout(() => socket.close(), 5000)
Debug
Known issues
breakingMessage events use 'recv' instead of standard 'message'.
fix
Use socket.on('recv') instead of socket.on('message').
affects: >=1.0.0
deprecatedThe 'connected' and 'disconnected' events are not part of standard WebSocket API.
fix
Use socket.on('connected') and socket.on('disconnected') as provided by the library.
affects: >=1.0.0
gotchasend() returns a promise that resolves when the message is actually sent, not when queued.
fix
Await the promise to ensure message is sent before proceeding.
affects: >=1.0.0
gotchaNo built-in TypeScript types; may cause type errors.
fix
Install @types/nanosocket or create a custom declaration file.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: socket.on is not a function
Missing emitterify dependency or incorrect import.
fix
Ensure emitterify is installed: npm install emitterify. Check import path.
TypeError: Cannot read property 'map' of undefined
Using socket.on('recv') without emitterify or in wrong context.
fix
Ensure emitterify is loaded and socket is initialized properly.
WebSocket connection to 'wss://...' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
WebSocket server is not running or URL is incorrect.
fix
Verify the WebSocket server URL and ensure it's accessible.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies
emitterifyrequiredUsed for event-based stream and callback handling (on('recv'), on('connected'), etc.)
Agent activity
14 hits · last 30 days
node
12
Amazon
1
OpenAI (training)
1
Resources
nanosocket — npm install nanosocket · libregistry