diff --git a/README.md b/README.md index 2eb8f34..82b8717 100644 --- a/README.md +++ b/README.md @@ -12,15 +12,15 @@ In `server.js`: const fastify = require('fastify')() fastify.register(require('fastify-ws'), { - wsLibrary: 'uws' // Use the uws library instead of the default ws library + library: 'uws' // Use the uws library instead of the default ws library }) fastify.ready(err => { if (err) throw err - fastify.wsServer - .on('connection', ws => { - ws.on('message', msg => ws.send(msg)) // Creates an echo server + fastify.ws + .on('connection', socket => { + socket.on('message', msg => socket.send(msg)) // Creates an echo server }) }) diff --git a/index.js b/index.js index 3b9e954..c693d98 100644 --- a/index.js +++ b/index.js @@ -3,16 +3,16 @@ const fp = require('fastify-plugin') module.exports = fp((fastify, opts, next) => { - const wsLib = opts.wsLibrary || 'ws' + const lib = opts.library || 'ws' - if (wsLib !== 'ws' && wsLib !== 'uws') return next(new Error('Invalid "wsLibrary" option')) + if (lib !== 'ws' && lib !== 'uws') return next(new Error('Invalid "library" option')) - const WebSocketServer = require(wsLib).Server + const WebSocketServer = require(lib).Server const wss = new WebSocketServer({ server: fastify.server }) - fastify.decorate('wsServer', wss) + fastify.decorate('ws', wss) next() }) diff --git a/test.js b/test.js index 459b0b7..e710a05 100644 --- a/test.js +++ b/test.js @@ -14,15 +14,15 @@ test('expose a WebSocket', t => { fastify.ready(err => { t.error(err) - fastify.wsServer - .on('connection', ws => { - ws.send('hello client') + fastify.ws + .on('connection', socket => { + socket.send('hello client') - ws.on('message', msg => { + socket.on('message', msg => { t.equal(msg, 'hello server') - ws.terminate() - fastify.wsServer.close(() => fastify.close()) + socket.terminate() + fastify.ws.close(() => fastify.close()) process.exit() })