# fastify-ws

> Basic WebSocket support for Fastify built on the ws and uws libraries.

Latest version **1.0.3** (published 2019-06-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install fastify-ws
pnpm add fastify-ws
yarn add fastify-ws
bun add fastify-ws
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2019-06-06 |
| First published | 2017-10-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 6.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 56 |
| Author | Gabe Jackson |
| Maintainers | 10 |
| Keywords | fastify, websocket, ws, uws |

## Links

- npm: https://www.npmjs.com/package/fastify-ws
- Repository: https://github.com/gj/fastify-ws
- Homepage: https://github.com/gj/fastify-ws#readme
- Issues: https://github.com/gj/fastify-ws/issues
- npm.io page: https://npm.io/package/fastify-ws

## Dependencies (5)

- [ws](https://npm.io/package/ws.md) ^6.1.4
- [uws](https://npm.io/package/uws.md) 10.148.1
- [bufferutil](https://npm.io/package/bufferutil.md) ^4.0.1
- [fastify-plugin](https://npm.io/package/fastify-plugin.md) ^1.5.0
- [utf-8-validate](https://npm.io/package/utf-8-validate.md) ^5.0.2

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 1.0.3 (latest) — 2019-06-06
- 1.0.2 — 2019-06-05
- 1.0.1 — 2019-03-04
- 1.0.0 — 2018-07-02
- 0.5.0 — 2018-05-11
- 0.4.1 — 2018-02-10
- 0.4.0 — 2017-10-30
- 0.3.1 — 2017-10-29
- 0.3.0 — 2017-10-28
- 0.2.0 — 2017-10-27
- 0.1.1 — 2017-10-19
- 0.1.0 — 2017-10-18

## README

# fastify-ws

[![Build Status](https://travis-ci.org/gj/fastify-ws.svg?branch=master)](https://travis-ci.org/gj/fastify-ws) [![npm version](https://badge.fury.io/js/fastify-ws.svg)](https://www.npmjs.com/package/fastify-ws)

WebSocket support for [Fastify](https://github.com/fastify/fastify) built on the [ws](http://npm.im/ws) and [uws](http://npm.im/uws) libraries.

## Example

In `server.js`:

```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:

```js
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:

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

## Notes

The creator of `uws` has ceased development on `uws` and started working on their new project, [uWebSockets.js](https://github.com/uNetworking/uWebSockets.js). If you want high-performance web socket support in Fastify, the last real release of `uws` (10.148.1) is probably your best bet, but given that it is now an abandoned project I can't recommend anyone use it for any non-throwaway projects. If you're using this library, I'd recommend you stick with the default `ws` option.

In addition, if you choose to use `uws` as your WebSocket library, ensure that you have configured your system properly and understand that the API is a slightly reduced subset of `ws`'s.

## License

Licensed under [MIT](./LICENSE).

---
_Source: https://npm.io/package/fastify-ws · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
