Registry / messaging / hapi-plugin-websocket

hapi-plugin-websocket

JSON →
library2.4.11jsnpmunverified

A HAPI plugin for seamless WebSocket integration, version 2.4.11 (stable). It allows HAPI routes to handle WebSocket connections by transforming messages into HTTP requests. Supports combined REST/WebSocket routes, exclusive WebSocket routes, authentication, custom subprotocols, and automatic pings. Requires Node.js >=12 and @hapi/hapi >=18.0.0. Ships TypeScript types.

npm install hapi-plugin-websocket
INSTALL
IMPORT
SIG · HAPI-PLUGIN-WEBSOC
H
hapi-plugin-websocket
messagingjavascriptv2.4.11
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.

default
const HAPIWebSocket = require('hapi-plugin-websocket')
import HAPIWebSocket from 'hapi-plugin-websocket'
Package is CommonJS; ESM import may fail depending on module resolution. Use require() or dynamic import().
Plugin
import type { Plugin } from 'hapi-plugin-websocket'
TypeScript type export for TypeScript users.
WebSocketRouteOptions
import type { WebSocketRouteOptions } from 'hapi-plugin-websocket'
TypeScript type for route options (e.g., plugins.websocket config).

Creates a HAPI server, registers the websocket plugin, and adds a route that echoes WebSocket messages.

const HAPI = require('@hapi/hapi'); const HAPIWebSocket = require('hapi-plugin-websocket'); (async () => { const server = new HAPI.Server({ port: 3000 }); await server.register(HAPIWebSocket); server.route({ method: 'GET', path: '/echo', config: { plugins: { websocket: true } }, handler: (request, h) => { const { mode, ws } = request.websocket(); if (mode === 'websocket') { ws.on('message', (msg) => ws.send(msg)); return ''; } return { message: 'Use WebSocket to connect.' }; } }); await server.start(); console.log('Server running on %s', server.info.uri); })();
Debug
Known issues
gotchaThe plugin requires `@hapi/hapi` >=18 and won't work with older hapi v17 or earlier.
fix
Use @hapi/hapi >=18.0.0.
affects: >=2.0.0
gotchaRoutes must have `config.plugins.websocket` set to enable WebSocket support; otherwise it's a normal HTTP route.
fix
Add `plugins: { websocket: true }` to route config.
affects: >=1.0.0
gotchaThe `request.websocket()` method returns an object; calling it on a non-WebSocket request will throw an error.
fix
Check `request.websocket()` existence or use `mode` to determine if it's a WebSocket request.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module '@hapi/hapi'
Missing peer dependency @hapi/hapi.
fix
Install required peer: `npm install @hapi/hapi`.
TypeError: server.register is not a function
Server not created correctly or hapi version mismatch.
fix
Use `const server = new HAPI.Server({...});` and ensure @hapi/hapi >=18.
AssertionError: plugins.websocket must be an object or true
Route config.plugins.websocket set to null or undefined.
fix
Set to `true` or an object with options.
Upgrade
Version history
2.4.11latest on npm
Audit
Dependencies
@hapi/hapirequiredpeer dependency: must be >=18.0.0 for plugin compatibility
wsrequiredruntime dependency for WebSocket server implementation
Agent activity
22 hits · last 30 days
node
20
OpenAI (training)
2
Resources
hapi-plugin-websocket — npm install hapi-plugin-websocket · libregistry