# @nano-utils/server-socket

> A lightweight wrapper library around the ws WebSocket API

Latest version **2.0.0** (published 2022-07-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install @nano-utils/server-socket
pnpm add @nano-utils/server-socket
yarn add @nano-utils/server-socket
bun add @nano-utils/server-socket
```

## 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 | 2.0.0 |
| Published | 2022-07-21 |
| First published | 2021-10-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 134.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Jason Xu |
| Maintainers | jason-xu |
| Keywords | WebSocket, Server |

## Links

- npm: https://www.npmjs.com/package/@nano-utils/server-socket
- Repository: https://github.com/JasonXu314/nano-utils.git#master
- Homepage: https://github.com/JasonXu314/nano-utils/tree/master#readme
- Issues: https://github.com/JasonXu314/nano-utils/issues
- npm.io page: https://npm.io/package/@nano-utils/server-socket

## Dependencies (2)

- [ws](https://npm.io/package/ws.md) ^8.2.3
- [@nano-utils/evt-src](https://npm.io/package/@nano-utils/evt-src.md) ^1.3.0

## 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

- 2.0.0 (latest) — 2022-07-21
- 1.1.0 — 2022-06-30
- 1.0.2 — 2021-10-18
- 1.0.1 — 2021-10-18
- 1.0.0 — 2021-10-15

## README

## Description

A lightweight wrapper library around the ws WebSocket API

## Installation

```
npm i @nano-utils/server-socket
```

or

```
yarn add @nano-utils/server-socket
```

## Usage

```js
import { Socket } from '@nano-utils/server-socket';
import WebSocket from 'ws';

const wss = new WebSocket.Server({ port: 3000 });

wss.on('connection', (sock) => {
	const socket = new Socket(sock);

	socket.on('MY_MESSAGE', (msg) => {
		console.log(`Type: ${msg.type}, Foo: ${msg.foo}`); // Type: MY_MESSAGE, Foo: something
	});
});
```

With Typescript:

```ts
import { Socket } from '@nano-utils/server-socket';
import WebSocket from 'ws';

type Msgs = {
	MY_MESSAGE: { type: 'MY_MESSAGE'; foo: string };
};

const wss = new WebSocket.Server({ port: 3000 }); // second type parameter is for outgoing messages

wss.on('connection', (sock) => {
	const socket = new Socket<Msgs, {}>(sock);

	socket.on('MY_MESSAGE', (msg) => {
		console.log(`Type: ${msg.type}, Foo: ${msg.foo}`); // Type: MY_MESSAGE, Foo: something
	});
});
```

Sending Messages:

```ts
import { Socket } from '@nano-utils/server-socket';
import WebSocket from 'ws';

type IMsgs = {
	CONNECTION: { type: 'CONNECTION'; id: string };
};

type OMsgs = {
	CONNECTED: { type: 'CONNECTED'; user: { id: string; name: string } };
};

const wss = new WebSocket.Server('wss://example.com');

wss.on('connection', (sock) => {
	const socket = new Socket<IMsgs, OMsgs>(sock);

	socket.on('CONNECTION', (msg) => {
		socket.send({ type: 'CONNECTED', user: { id: msg.id, name: 'foo' } });
	});
});
```

Awaiting Messages:

```ts
import { Socket } from '@nano-utils/server-socket';
import WebSocket from 'ws';

type IMsgs = {
	CONNECTION: { type: 'CONNECTION'; id: string };
};

type OMsgs = {
	CONNECTED: { type: 'CONNECTED'; user: { id: string; name: string } };
};

const wss = new WebSocket.Server('wss://example.com');

wss.on('connection', async (sock) => {
	const socket = new Socket<IMsgs, OMsgs>(sock);

	const msg = await socket.await('CONNECTION');

	socket.send({ type: 'CONNECTED', user: { id: msg.id, name: 'foo' } });
});
```

## Usage Notes:

-   Each message (both outgoing and incoming) must have a type property, which is litstened to in the `on` method
-   If using typescript, each message's key in the message type map must match the type property of the message
-   This package is designed to be paired with the [web-sockets](https://npmjs.org/packages/@nano-utils/web-sockets) library on the frontend, but any client that sends messages in json format with type properties will work

---
_Source: https://npm.io/package/@nano-utils/server-socket · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
