Registry / payments / khenshin-js-protocol

khenshin-js-protocol

JSON →
library1.0.59jsnpmunverified

A JavaScript/TypeScript library implementing the Khenshin websocket protocol for real-time communication with Khenshin payment services. Current stable version is 1.0.59. The library provides WebSocket-based messaging with automatic reconnection, message validation, and TypeScript type definitions. It is actively maintained by the Khenshin team, with frequent releases addressing protocol updates and bug fixes. Differentiates from generic WebSocket clients by offering domain-specific message types and built-in handling of Khenshin's authentication and session management.

npm install khenshin-js-protocol
INSTALL
IMPORT
SIG · KHENSHIN-JS-PROTOC
K
khenshin-js-protocol
paymentsjavascriptv1.0.59
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.

KhenshinProtocol
import { KhenshinProtocol } from 'khenshin-js-protocol'
const KhenshinProtocol = require('khenshin-js-protocol');
Library is ESM-only since v1.0. CommonJS require will not work.
MessageType
import { MessageType } from 'khenshin-js-protocol'
import { MessageTypes } from 'khenshin-js-protocol'
The correct enum name is MessageType (singular), not MessageTypes.
createConnection
import { createConnection } from 'khenshin-js-protocol'
import createConnection from 'khenshin-js-protocol'
createConnection is a named export, not a default export.

Demonstrates creating a WebSocket connection, handling open/message/error events, and sending a PING message.

import { createConnection, MessageType } from 'khenshin-js-protocol'; const wsUrl = process.env.KHENSHIN_WS_URL ?? 'wss://example.com/ws'; const apiKey = process.env.KHENSHIN_API_KEY ?? ''; async function main() { const connection = createConnection(wsUrl, { apiKey }); connection.on('open', () => { console.log('WebSocket connected'); connection.send({ type: MessageType.PING, payload: {} }); }); connection.on('message', (data) => { if (data.type === MessageType.PONG) { console.log('Received pong'); } }); connection.on('error', (err) => { console.error('Connection error:', err); }); await connection.connect(); } main().catch(console.error);
Debug
Known issues
gotchaThe library does not automatically reconnect on WebSocket close. You must implement reconnection logic yourself or use the 'reconnect' option if available.
fix
Wrap the connection logic in a loop with exponential backoff, or upgrade to version 1.0.60+ which may include built-in reconnection.
affects: >=1.0.0 <1.0.60
gotchaThe payload property in messages is required and must be an object. Sending a string or number directly will cause validation errors.
fix
Always wrap primitive values in an object, e.g., { payload: { id: 123 } }.
affects: >=1.0.0
deprecatedThe 'onMessage' event handler is deprecated in favor of the 'message' event.
fix
Use connection.on('message', handler) instead of connection.onMessage(handler).
affects: >=1.0.40 <1.0.59
breakingIn version 1.0.50, the default export was removed. All exports are now named.
fix
Change 'import Khenshin from ...' to '{ KhenshinProtocol } from ...'. For 'createConnection', use named import.
affects: >=1.0.50
Errors
Common errors & fixes
TypeError: KhenshinProtocol is not a constructor
Using default import in TypeScript when the library only exports named exports.
fix
Use { KhenshinProtocol } from 'khenshin-js-protocol'
Error: Validation failed: payload is required
Sending a message without a payload property.
fix
Always include a payload object, e.g., send({ type: 'PING', payload: {} })
WebSocket connection to 'wss://...' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
WebSocket server is not running or URL is incorrect.
fix
Check that the WebSocket server is running and the URL is correct. Ensure no firewall blocks the connection.
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'apiKey')
Options object passed to createConnection is missing required fields.
fix
Provide apiKey and other required options. Example: createConnection(url, { apiKey: '...' })
Upgrade
Version history
1.0.59latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
17 hits · last 30 days
node
16
Resources
khenshin-js-protocol — npm install khenshin-js-protocol · libregistry