# aws-websocket-handler

> This module is created to handle AWS Lambda websocket actions as a one default handler

Latest version **1.0.1** (published 2021-01-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install aws-websocket-handler
pnpm add aws-websocket-handler
yarn add aws-websocket-handler
bun add aws-websocket-handler
```

## 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.1 |
| Published | 2021-01-21 |
| First published | 2021-01-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 266.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Bogusz Przybyslawski |
| Maintainers | matrus2 |
| Keywords | AWS, Lambda, Websocket, API Gateway |

## Links

- npm: https://www.npmjs.com/package/aws-websocket-handler
- Repository: https://github.com/matrus2/aws-websocket-handler
- Homepage: https://github.com/matrus2/aws-websocket-handler#readme
- Issues: https://github.com/matrus2/aws-websocket-handler.git
- npm.io page: https://npm.io/package/aws-websocket-handler

## 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.1 (latest) — 2021-01-21
- 1.0.0 — 2021-01-20

## README

# aws-websocket-handler
[![TypeScript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-%230074c1.svg)](http://www.typescriptlang.org/)

This module is created to handle AWS Lambda websocket actions as a one default handler. There are multiple benefits of doing so:
- all actions are handled by one lambda function;
- no cold start for actions, which are rarely used;
- the codebase is sharable accross all actions;

### How it works?

When you want to use WebSocket API in API Gateway, which are integrated with Lambda functions it is required to specify two mandatory routes $connect and $disconnect. The approach is presented in detail [here](https://aws.amazon.com/blogs/compute/announcing-websocket-apis-in-amazon-api-gateway/). Instead of specifing all actions as separate functions we use here a power of $default route, which is invoked every time no matching expresion is found. This package use `action` parameter from payload.

#### How to use?

1. Install the package:
```
npm i aws-websocket-handler
```

2. Use in your lambda handler:

```
// Typescript
import { APIGatewayProxyEvent, Context } from 'aws-lambda';
import { Configuration, websocketHandler } from 'aws-websocket-handler';
import { validateToken } from './middlewares/validateToken';
import { simpleAction } from './actions/simple';

const config: Configuration = {
    actions: [
        {
            name: 'simple',
            handler: simpleAction,
        },
    ],
    middlewares: [validateToken],
};

exports.handler = async (
    event: APIGatewayProxyEvent,
    context: Context
): Promise<AWSLambda.APIGatewayProxyResult | AWSLambda.APIGatewayProxyStructuredResultV2> => {
    return websocketHandler(event, context, config);
};

// Javascript
const { websocketHandler } = require("aws-websocket-handler");
const { validateToken } = require("./middlewares/validateToken");
const { simpleAction } = require("./actions/simple");
const config = {
    actions: [
        {
            name: 'simple',
            handler: simpleAction,
        }
    ],
    middlewares: [validateToken],
};
exports.handler = async (event, context) => {
    return websocketHandler(event, context, config);
};

```

#### Configuration:

| Param  | Description | Required
| ------------- | ------------- | ------------- |
| actions | Array of ActionsHandlers | optional 
| middlewares  | Array of Middlewares | optional
| fallback  | Fallback promise to be invoked if no action expression is found | optional
| enableLogging  | Boolean param to enable simple logging | optional

##### ActionsHandler example

Name corresponds to `Event.body.action`, if the expression matches, the handler function is invoked. 

```
const simpleAction = async (event, context, runData) => {
    console.log(runData);
    return {};
};

```
##### Middleware example

```
const validateToken = async (event, _context, runData) => {
    const { token } = JSON.parse(event.body);
    if (!token)
        throw new Error('No token');
    let tokenData;
    try {
        tokenData = JWT.verify(token, config_1.KEY);
    }
    catch (e) {
        throw new Error(e.message);
    }
    return { ...runData, tokenData };
};

```

##### Fallback
```
const fallback = async () => {
  return { statusCode: 200, body: 'fallback' }
},
```

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