Skip to content

oransel/fastify-simple-ws

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fastify-simple-ws

Build Status npm version

WebSocket support for Fastify built on the blazing fast ws library (removed uws support).

Example

In server.js:

'use strict'

const fastify = require('fastify')()

fastify.register(require('fastify-ws'))

fastify.ready(err => {
  if (err) throw err

  console.log('Server started.')

  fastify.ws
    .on('connection', socket => {
      console.log('Client connected.')

      socket.on('message', msg => socket.send(msg)) // Creates an echo server

      socket.on('close', () => console.log('Client disconnected.'))
    })
})

fastify.listen(34567)

Then run node server.js and navigate to http://localhost:34567 in your browser. In the browser's JavaScript console, open a client-side WebSocket connection:

const host = location.origin.replace(/^http/, 'ws')
const ws = new WebSocket(host)
ws.onmessage = msg => console.log(msg.data)

Then, still in the browser console, send some messages to the server and watch as they're echoed back to you:

ws.send('WebSockets are awesome!')
// => undefined
// LOG: WebSockets are awesome!

Notes

This forked version removes the support for uws.

License

Licensed under MIT.

About

Simple WebSocket support for Fastify.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%