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);
})();
Errors
Common errors & fixes
Error: Cannot find module '@hapi/hapi'
Missing peer dependency @hapi/hapi.
fixInstall required peer: `npm install @hapi/hapi`.
TypeError: server.register is not a function
Server not created correctly or hapi version mismatch.
fixUse `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.
fixSet to `true` or an object with options.
Audit
Dependencies
@hapi/hapirequiredpeer dependency: must be >=18.0.0 for plugin compatibility
wsrequiredruntime dependency for WebSocket server implementation