Registry / messaging / websocket-close-codes

websocket-close-codes

JSON →
library1.0.0jsnpmunverified

A TypeScript library providing a strongly-typed enum for WebSocket connection close codes as defined in RFC 6455. Version 1.0.0 is the current stable release. It offers a single enum (CloseCode) with all standard close codes (NORMAL_CLOSURE, GOING_AWAY, PROTOCOL_ERROR, etc.) and maps them to their numeric values. Unlike plain string or number constants, this library provides compile-time type checking and autocomplete in IDEs, reducing errors from mistyped magic numbers. Suitable for both Node.js and browser environments with ESM and CommonJS support. No dependencies. Ships TypeScript declaration files.

npm install websocket-close-codes
INSTALL
IMPORT
SIG · WEBSOCKET-CLOSE-CO
W
websocket-close-codes
messagingjavascriptv1.0.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.

CloseCode
import { CloseCode } from 'websocket-close-codes'
import CloseCode from 'websocket-close-codes'
CloseCode is a named export, not a default export. Use curly braces.
CloseCode (CommonJS)
const { CloseCode } = require('websocket-close-codes')
const CloseCode = require('websocket-close-codes')
CommonJS requires destructuring to get the named export.
CloseCode (namespace import)
import * as wscc from 'websocket-close-codes'; wscc.CloseCode
import { default as wscc } from 'websocket-close-codes'
A wildcard import works if you prefer a namespace. There is no default export.

Demonstrates importing the CloseCode enum and using it to close a WebSocket connection with a standard code.

import { CloseCode } from 'websocket-close-codes'; const socket = new WebSocket('wss://echo.websocket.org'); socket.addEventListener('open', () => { console.log('Connected'); // Normal closure after a short delay setTimeout(() => { socket.close(CloseCode.NORMAL_CLOSURE, 'Done with test'); }, 1000); }); socket.addEventListener('close', (event) => { console.log(`Closed with code ${event.code}: ${event.reason}`); if (event.code === CloseCode.NORMAL_CLOSURE) { console.log('Clean exit'); } }); socket.addEventListener('error', (err) => { console.error('WebSocket error', err); });
Debug
Known issues
gotchaThe CloseCode enum is zero-based but WebSocket close code 0 is not allowed per spec; use NORMAL_CLOSURE (1000) as the minimum.
fix
Always use one of the defined enum values, not raw numbers like 0.
affects: >=1.0.0
gotchaThe enum includes codes reserved for the WebSocket protocol (e.g., 1004 is reserved) which are not defined in the enum; be careful not to treat undefined enum members as valid.
fix
Only use the named members from CloseCode (1000-1003, 1005, etc.) as defined in the documentation.
affects: >=1.0.0
gotchaCloseCode.COMBINATION_RESERVED (3000) is not a standard code; it is part of reserved private range (3000-3999). Misuse may cause interoperability issues.
fix
Use only standard codes (1000-1015) unless you control both endpoints and have agreed on private codes.
affects: >=1.0.0
Errors
Common errors & fixes
Property 'NORMAL_CLOSURE' does not exist on type 'typeof import("websocket-close-codes")'
Trying to import the enum as a default export instead of named export.
fix
Use `import { CloseCode } from 'websocket-close-codes';`
Module '"websocket-close-codes"' has no default export
Using require without destructuring in CommonJS.
fix
Use `const { CloseCode } = require('websocket-close-codes');`
An enum member has initializer '1000'. Did you mean to use it as a numeric literal?
When writing TypeScript code, attempts to reassign enum members or treat them as variables.
fix
Do not try to change enum values; they are read-only constants. Use the enum directly: `CloseCode.NORMAL_CLOSURE`
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
34 hits · last 30 days
node
30
OpenAI (training)
1
Resources
websocket-close-codes — npm install websocket-close-codes · libregistry