{"id":47819114,"url":"https://github.com/somod-dev/somod-websocket-extension","last_synced_at":"2026-04-03T19:01:42.171Z","repository":{"id":211116492,"uuid":"728175733","full_name":"somod-dev/somod-websocket-extension","owner":"somod-dev","description":null,"archived":false,"fork":false,"pushed_at":"2025-07-05T13:23:18.000Z","size":247,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-12T12:39:42.170Z","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/somod-dev.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,"governance":null}},"created_at":"2023-12-06T11:45:30.000Z","updated_at":"2025-07-05T13:23:21.000Z","dependencies_parsed_at":"2023-12-06T15:52:35.063Z","dependency_job_id":null,"html_url":"https://github.com/somod-dev/somod-websocket-extension","commit_stats":null,"previous_names":["somod-dev/somod-websocket-extension"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/somod-dev/somod-websocket-extension","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/somod-dev%2Fsomod-websocket-extension","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/somod-dev%2Fsomod-websocket-extension/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/somod-dev%2Fsomod-websocket-extension/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/somod-dev%2Fsomod-websocket-extension/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/somod-dev","download_url":"https://codeload.github.com/somod-dev/somod-websocket-extension/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/somod-dev%2Fsomod-websocket-extension/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31371636,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-03T17:53:18.093Z","status":"ssl_error","status_checked_at":"2026-04-03T17:53:17.617Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":"2026-04-03T19:01:26.811Z","updated_at":"2026-04-03T19:01:42.128Z","avatar_url":"https://github.com/somod-dev.png","language":"TypeScript","readme":"# SOMOD WebSocket Extension\n\n---\n\nThe [SOMOD](https://somod.dev) Extension Configure WebSocket routes and validate incoming WebSocket Messages in Serverless Functions.\n\n\u003e The middlware in this extension works with Functions of type `WebSocket` Only.\n\n## Install\n\nInstall as an npm package in somod modules\n\n```bash\nnpm install somod-websocket-extension\n```\n\n## Usage\n\n### Attach the middleware to the Serverless Function\n\n```yaml\nResources:\n  MyWebSocketMessageHandler:\n    Type: AWS::Serverless::Function\n    Properties:\n      CodeUri:\n        SOMOD::Function:\n          # ...\n          middlewares:\n            - module: somod-websocket-extension\n              resource: SomodWebSocketMiddleware\n      # ...\n```\n\nRefer to SOMOD's [Middleware](https://docs.somod.dev/reference/main-concepts/serverless/middlewares) reference for more information\n\n### Configure Routes and Schemas\n\nRoutes configuration for each serverless function can be provided using a yaml file at `serverless/functions/\u003cfunction_name\u003e.websocket.yaml`.\n\nExample:\n\n```yaml\n# serverless/functions/chat.websocket.yaml\n\n$default:\n  body:\n    parser: json\n    schema:\n      type: object\n      required: [name]\n      properties:\n        name:\n          type: string\n          maxLength: 32\n        email:\n          type: string\n          format: email\n```\n\n### Access Sanitized Message\n\nThe sanitized Message can be accessed using the [middleware](https://docs.somod.dev/reference/main-concepts/serverless/middlewares)'s context using the **`somod-websocket-message`** key.\n\nExample:\n\n```typescript\n// serverless/functions/chat.ts\n\nconst ChatHandler = event =\u003e {\n  const message = event.somodMiddlewareContext.get(\"somod-websocket-message\");\n  // use message to read the data from the incoming websocket message\n};\n\nexport default ChatHandler;\n```\n\nThis module also provides a utility library to create Serverless Functions with multiple routes easily. Refer to [RouteBuilder](#routeBuilder) for more details\n\n## Specification\n\n### Structure of Routes configuration file\n\nIn general, routes configuration in `\u003cfunction_name\u003e.websocket.yaml` file follows the below structure\n\n```yaml\n\u003crouteKey\u003e:\n  body:\n    parser?: \u003c\"text\"|\"json\"|\"formdata\"\u003e. If not provided, automatically choosen based on the Content-Type Header (text is considered if automatic detection fails)\n    schema: \u003cjson schema\u003e\n```\n\nThe complete JSONSchema is available [here](/lib/routes-schema.ts)\n\n### Type of the Message object\n\nThe message object accessed using `event.somodMiddlewareContext.get(\"somod-websocket-message\")` has this type.\n\n```typescript\ntype Message\u003cT = unknown\u003e = {\n  routeKey: string;\n  body: T;\n};\n```\n\nThe `Message` Type is available from this module to use (import as shown below)\n\n```typescript\nimport { Message } from \"somod-websocket-extension\";\n```\n\n### Error Handling\n\nThe middleware immediates ends the Lambda execution and returns appropriate message when Validation fails\n\n## RouteBuilder\n\nThe `RouteBuilder` is a wrapper javascript utility library to create serverless functions with multiple routes.\n\n### Using the RouteBuilder\n\n```typescript\n// serverless/function/user.ts\nimport { RouteBuilder } from \"somod-websocket-extension\";\n\nconst builder = new RouteBuilder();\n\nbuilder.add(\"$default\", defaultMessageHandler);\n\nexport default builder.getHandler();\n```\n\n### RouteBuilder Specification\n\nRouteBuilder has 2 methods\n\n- `add`\n\n  ```typescript\n  function add(\n    routeKey: string,\n    handler: (message: Message, event: RawEvent) =\u003e Promise\u003cResponse\u003e\n  ): void {\n    //\n  }\n  ```\n\n  The handler receives the [sanitized message object](#type-of-the-message-object) and the raw event from AWS. The handler has to return a promise which resolves to the Response object.\n\n  \u003e The Raw Event type and Response type is documented [here](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html) in the AWS specification.\n\n- `getHandler`\n\n  ```typescript\n  function getHandler(): (event: RawEvent) =\u003e Promise\u003cResponse\u003e {\n    //\n  }\n  ```\n\n  handle function returns the function which is a lambda function handler\n\n## Issues\n\nThe project issues, features, and milestones are maintained in this GitHub repo.\n\nCreate issues or feature requests at https://github.com/somod-dev/somod-websocket-extension/issues\n\n## Contributions\n\nPlease read our [CONTRIBUTING](https://github.com/somod-dev/somod/blob/main/CONTRIBUTING.md) guide before contributing to this project.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsomod-dev%2Fsomod-websocket-extension","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsomod-dev%2Fsomod-websocket-extension","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsomod-dev%2Fsomod-websocket-extension/lists"}