npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@teleology/lambda-ws

v1.1.0

Published

Lambda Websocket wrapper, creating a message publisher and auto inflation of incoming data

Downloads

6

Readme

@teleology/lambda-ws

Downloads Version License

A Serverless >= 1.38 Lambda Websocket wrapper. Auto parses data, injects a publish method as well as a quick eventType reference.

Installation

npm install @teleology/lambda-ws

or

yarn add @teleology/lambda-ws

Usage

Servereless Project

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
Wrapper
// 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);
  }
});

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.

Connection Details

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);
})
Connection Details

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);
}

EnhancedEvent

type Publisher = Function(data: any);

type EnhancedEvent & OriginalEvent {
  action: connect | disconnect | message
  publish: Publisher
  data: JSON | Buffer | string
}