nes adds native WebSocket support to hapi-based application servers. Instead of treating the WebSocket connections as a separate platform with its own security and application context, nes builds on top of the existing hapi architecture to provide a flexible and organic extension.
Protocol version: 2.4.x (different from module version)
Lead Maintainer - Matt Harrison
API
The full client and server API is available in the API documentation.
Protocol
The nes protocol is described in the Protocol documentation.
Examples
Route invocation
Server
const Hapi = ;const Nes = ; const server = ; const start = async { await server; server; await serverstart;}; ;
Client
const Nes = ; var client = 'ws://localhost'; const start = async { await client; const payload = await client; // Can also request '/h' // payload -> 'world!'}; ;
Subscriptions
Server
const Hapi = ;const Nes = ; const server = ; const start = async { await server; server; await serverstart; server; server;}; ;
Client
const Nes = ; const client = 'ws://localhost';const start = async { await client; const handler = { // update -> { id: 5, status: 'complete' } // Second publish is not received (doesn't match) }; client;}; ;
Broadcast
Server
const Hapi = ;const Nes = ; const server = ; const start = async { await server; await serverstart; server;}; ;
Client
const Nes = ; const client = 'ws://localhost';const start = async { await client; client { // update -> 'welcome!' };}; ;
Route authentication
Server
const Hapi = ;const Basic = ;const Bcrypt = ;const Nes = ; const server = ; const start = async { await server; // Set up HTTP Basic authentication const users = john: username: 'john' password: '$2a$10$iqJSHD.BGr0E2IxQwYgJmeP3NvhPrXAeLSaGCj6IR/XU5QtjVu5Tm' // 'secret' name: 'John Doe' id: '2133d32a' ; const validate = async { const user = usersusername; if !user return isValid: false ; const isValid = await Bcrypt; const credentials = id: userid name: username ; return isValid credentials ; }; serverauth; // Configure route with authentication server; await serverstart;}; ;
Client
const Nes = ; const client = 'ws://localhost';const start = async { await client; const payload = await client // Can also request '/h' // payload -> 'Hello John Doe'}; ;
Subscription filter
Server
const Hapi = ;const Basic = ;const Bcrypt = ;const Nes = ; const server = ; const start = async { await server; // Set up HTTP Basic authentication const users = john: username: 'john' password: '$2a$10$iqJSHD.BGr0E2IxQwYgJmeP3NvhPrXAeLSaGCj6IR/XU5QtjVu5Tm' // 'secret' name: 'John Doe' id: '2133d32a' ; const validate = async { const user = usersusername; if !user return isValid: false ; const isValid = await Bcrypt; const credentials = id: userid name: username ; return isValid credentials ; }; serverauth; // Set up subscription server; await serverstart; server; server;}; ;
Client
const Nes = ; const client = 'ws://localhost'; // Authenticate as 'john' const start = async { await client; const handler = { // First publish is not received (filtered due to updater key) // update -> { id: 6, status: 'initial', updater: 'steve' } }; client;}; ;
Browser Client
When you require('nes')
it loads the full module and adds a lot of extra code that is not needed
for the browser. The browser will only need the nes client. If you are using CommonJS you can
load the client with require('nes/client')
.