A Serverless >= 1.38 Lambda Websocket wrapper. Auto parses data, injects a publish method as well as a quick eventType reference.
npm install @teleology/lambda-ws
or
yarn add @teleology/lambda-ws
Configure your function to accept all routes/actions.
functions:
websocket:
handler: src/index.default
memorySize: 3008
events:
- websocket:
route: $connect
- websocket:
route: $disconnect
- websocket:
route: $default
Be aware, you do not need to return anything within the lambda, the library will automatically return a valid response. However for a custom response your handler should return a JSON object with a statusCode.
// pre-ES6
// const lambdaWs = require('@teleology/lambda-ws');
// ES6
import lambdaWs from '@teleology/lambda-ws';
const actionMap = {
connect: async (event) => {},
disconnect: async (event) => {},
message: async ({ data, publish }) => {
// console log - echo back data
console.log('received', data);
await publish(data);
},
};
export default lambdaWs(async (event) => {
const { action } = event;
const actionHandler = actionMap[action];
if (actionHandler) {
return actionHandler(event);
}
});
Connection data is needed to re-establish a publish request and can be used outside of the context of the lambda (as long as the connection is still open).
import lambdaWs, { extractConnectionData } from '@teleology/lambda-ws';
export default lambdaWs(async (event) => {
// Save connectionData for use later
const connectionData = extractConnectionData(event);
})
If you have stored your connection details somewhere else, you can publish data to a client outside of the websocket lambda.
import { createPublisher } from '@teleology/lambda-ws';
const publishToClient = async (connectionData, message) => {
const publish = createPublisher(connectionData);
return publish(message);
}
type Publisher = Function(data: any);
type EnhancedEvent & OriginalEvent {
action: connect | disconnect | message
publish: Publisher
data: JSON | Buffer | string
}