
Security News
Deno 2.4 Brings Back deno bundle, Improves Dependency Management and Observability
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.
aws-websocket-handler
Advanced tools
This module is created to handle AWS Lambda websocket actions as a one default handler
This module is created to handle AWS Lambda websocket actions as a one default handler. There are multiple benefits of doing so:
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. 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.
npm i aws-websocket-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);
};
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 |
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 {};
};
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 };
};
const fallback = async () => {
return { statusCode: 200, body: 'fallback' }
},
FAQs
This module is created to handle AWS Lambda websocket actions as a one default handler
We found that aws-websocket-handler demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.
Security News
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.