Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

lambda-websocket

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lambda-websocket

An API Gateway Lambda wrapper that mimics server websockets

  • 1.0.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8
Maintainers
1
Weekly downloads
 
Created
Source

lambda-websocket

Downloads Version License

Note: This library is no longer maintained, it has been moved to the following repo.

@lawcket/websocket

An API Gateway Lambda wrapper that mimics server websockets

Installation

npm install lambda-websocket

or

yarn add lambda-websocket

Usage

To create a lambda-websocket, import the library and create a new instance of the LambdaWebSocket. Add event listener methods as you wish and then call createHandler to export the result as your lambdas handler.

import LambdaWebSocket from 'lambda-websocket';

const lambdaSocket = new LambdaWebSocket();

lambdaSocket.on('connect', async (context) => {
    console.log('Client requested connection', context);
    const { headers } = context;
    // basic authorization header check
    if (!headers.Authorization) {
        return {
            statusCode: "401"
        }
    }
});

lambdaSocket.on('message', async ({ send, message }) => {
    console.log('Client sent message', message);

    // send message to client
    await send({ message: 'hello' })
    
    // The lambda will automatically return a 200, 
    // but to override that return a response here.
    // We will pretend that in this response we have created
    // something.
    return {
        statusCode: "201",
    };
});

lambdaSocket.on('close', async (context) => {
    console.log('Client went away'); 
});

export default lambdaSocket.createHandler();
Event Listeners
EventParamsDescription
connectContextClient is requesting a connection
closeContextClient was disconnected or requested a close
messageContextNormal client communication
Context Definition
NameTypeDefaultsDescription
headersObject{}The headers passed from the client
messageObject{}The message sent from the client, will try to auto-parse JSON
connectionIDStringThe ID needed to connect to ApiGatewayManagementApi
endpointStringconstructedThe endpoint needed to connect to ApiGatewayManagementApi
domainString
stageString
typeStringApi Gateway eventType: DISCONNECTED, CONNECTED, MESSAGE
sendFunctionSend messages to the client. Non-Buffer items will be stringified
Caveats

This library should be used in conjunction with the library serverless-websockets-plugin with a full example project here. It is not a complete websocket implementation and relies on the plugin to handle the API Gateway configuration.

Keywords

FAQs

Package last updated on 10 Feb 2019

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc