{"id":20031806,"url":"https://github.com/matrus2/aws-websocket-handler","last_synced_at":"2025-03-02T05:24:47.034Z","repository":{"id":57188489,"uuid":"329455438","full_name":"matrus2/aws-websocket-handler","owner":"matrus2","description":null,"archived":false,"fork":false,"pushed_at":"2021-01-21T22:10:40.000Z","size":9,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-12T18:46:40.905Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/matrus2.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-01-13T23:19:02.000Z","updated_at":"2021-09-22T23:14:36.000Z","dependencies_parsed_at":"2022-08-28T13:00:45.412Z","dependency_job_id":null,"html_url":"https://github.com/matrus2/aws-websocket-handler","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matrus2%2Faws-websocket-handler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matrus2%2Faws-websocket-handler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matrus2%2Faws-websocket-handler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matrus2%2Faws-websocket-handler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/matrus2","download_url":"https://codeload.github.com/matrus2/aws-websocket-handler/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241462666,"owners_count":19966904,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T09:34:47.251Z","updated_at":"2025-03-02T05:24:47.013Z","avatar_url":"https://github.com/matrus2.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# aws-websocket-handler\n[![TypeScript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-%230074c1.svg)](http://www.typescriptlang.org/)\n\nThis module is created to handle AWS Lambda websocket actions as a one default handler. There are multiple benefits of doing so:\n- all actions are handled by one lambda function;\n- no cold start for actions, which are rarely used;\n- the codebase is sharable accross all actions;\n\n### How it works?\n\nWhen 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.\n\n#### How to use?\n\n1. Install the package:\n```\nnpm i aws-websocket-handler\n```\n\n2. Use in your lambda handler:\n\n```\n// Typescript\nimport { APIGatewayProxyEvent, Context } from 'aws-lambda';\nimport { Configuration, websocketHandler } from 'aws-websocket-handler';\nimport { validateToken } from './middlewares/validateToken';\nimport { simpleAction } from './actions/simple';\n\nconst config: Configuration = {\n    actions: [\n        {\n            name: 'simple',\n            handler: simpleAction,\n        },\n    ],\n    middlewares: [validateToken],\n};\n\nexports.handler = async (\n    event: APIGatewayProxyEvent,\n    context: Context\n): Promise\u003cAWSLambda.APIGatewayProxyResult | AWSLambda.APIGatewayProxyStructuredResultV2\u003e =\u003e {\n    return websocketHandler(event, context, config);\n};\n\n// Javascript\nconst { websocketHandler } = require(\"aws-websocket-handler\");\nconst { validateToken } = require(\"./middlewares/validateToken\");\nconst { simpleAction } = require(\"./actions/simple\");\nconst config = {\n    actions: [\n        {\n            name: 'simple',\n            handler: simpleAction,\n        }\n    ],\n    middlewares: [validateToken],\n};\nexports.handler = async (event, context) =\u003e {\n    return websocketHandler(event, context, config);\n};\n\n```\n\n#### Configuration:\n\n| Param  | Description | Required\n| ------------- | ------------- | ------------- |\n| actions | Array of ActionsHandlers | optional \n| middlewares  | Array of Middlewares | optional\n| fallback  | Fallback promise to be invoked if no action expression is found | optional\n| enableLogging  | Boolean param to enable simple logging | optional\n\n##### ActionsHandler example\n\nName corresponds to `Event.body.action`, if the expression matches, the handler function is invoked. \n\n```\nconst simpleAction = async (event, context, runData) =\u003e {\n    console.log(runData);\n    return {};\n};\n\n```\n##### Middleware example\n\n```\nconst validateToken = async (event, _context, runData) =\u003e {\n    const { token } = JSON.parse(event.body);\n    if (!token)\n        throw new Error('No token');\n    let tokenData;\n    try {\n        tokenData = JWT.verify(token, config_1.KEY);\n    }\n    catch (e) {\n        throw new Error(e.message);\n    }\n    return { ...runData, tokenData };\n};\n\n```\n\n##### Fallback\n```\nconst fallback = async () =\u003e {\n  return { statusCode: 200, body: 'fallback' }\n},\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatrus2%2Faws-websocket-handler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmatrus2%2Faws-websocket-handler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatrus2%2Faws-websocket-handler/lists"}