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

aws-websocket-server

v0.1.0

Published

AWS WebSocket Wrapper to provide socket.io like interface to NodeJS serverless handlers

Downloads

1

Readme

aws-websocket-server

Info

Add Socket.io like wrapper to AWS WebSocket server(NodeJS serverless)
This package uses MongoDB as database to record connection & channel subscription.

Download

npm install aws-websocket-server

Requirements

- Vaild MongoDB Url
- API WEBSOCKET Endpoint from AWS
- AWS Websocket client wrapper to communicate with client using server wrapper.
  npm install aws-websocket-client

Github:- https://github.com/racop/aws-websocket-client

How to Use

import AWSWebSocketServer from "aws-websocket-server";
const DB_URI = "mongodb+srv://username:password@connection_url"; // You mongodb connection url
const socket = new AWSWebSocketServer({
  mongoDBURI: DB_URI,
  socketConnectionModel = SocketConnection, // Optional, By default uses ./models/socketConnection.model.js
  socketChannelModel = SocketChannel, // Optional, By default uses ./models/socketChannel.model.js
  debug: false,
})

Inside handlers

On connectHandler

module.exports.connectHandler = (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false;
  socket.addConnection(event.requestContext.connectionId) // Register connection in socket
    .then(() => {
      // do something like subscribe to channels
      socket.subscribe('global', event.requestContext.connectionId); // Subscribed to channel name global
      callback(null, successfullResponse)
    })
    .catch((err) => {
      callback({statusCode: 500,body: JSON.stringify(err)})
    })
}

On disconnectHandler

module.exports.disconnectHandler = (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false;
  socket.deleteConnection(event.requestContext.connectionId) // DeRegister connection in socket
    .then(() => {
      callback(null, successfullResponse)
    })
    .catch((err) => {
      console.log(err)
      callback(failedResponse(500, JSON.stringify(err)))
    })
}

On subscribeHandler

module.exports.subscribeHandler = (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false;
  const channelName = JSON.parse(event.body).data;
  socket.subscribe(channelName, event.requestContext.connectionId) // Subscribe the channel
    .then(() => {
      callback(null, successfullResponse)
    })
    .catch((err) => {
      console.log(err)
      callback(failedResponse(500, JSON.stringify(err)))
    })
}

On unsubscribeHandler

module.exports.unsubscribeHandler = (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false;
  const channelName = JSON.parse(event.body).data;
  socket.subscribe(channelName, event.requestContext.connectionId) // Unsubscribe the channel
    .then(() => {
      callback(null, successfullResponse)
    })
    .catch((err) => {
      console.log(err)
      callback(failedResponse(500, JSON.stringify(err)))
    })
}

On sendMessageHandler

module.exports.sendMessageHandler = async (event, context) => {
  context.callbackWaitsForEmptyEventLoop = false;
  const connectionId = event.requestContext.connectionId; // Client connectionId

  const data = JSON.parse(event.body).data;
  const eventListener = data.event; // EventListener received from client
  const messageBody = data.body; // Received Data from client

  // Do something based on the eventListener and data received from client
  await socket.in("global").emit(event, 'online', messageBody); // Send message to all clients subscribed to global channel on online listener.
  return { statusCode: 200, body: 'Data sent.' };
};

Emit CheatSheet

socket.broadcast().emit(event, listener, message); // Send message to all connected clients with on provided listener
Eg:-
// This can be used to inform all clients listening to online listener about Bob
await socket.broadcast().emit(event, 'online', {name:"Bob"}); 


await socket.to("global").emit(event, 'friend_request', message); // Send message to all clients subscribed to global channel except sender
Eg:-
// Send message to all clients subscribed to global channel on frined_request listener except sender.
await socket.to("global").emit(event, 'friend_request', {id:1, name: "John"}); 


await socket.to("global").emit(event, 'friend_request', message); // Send message to all clients subscribed to global channel including sender
Eg:-
// Send message to all clients subscribed to global channel on frined_request listener including sender.
await socket.to("global").emit(event, 'friend_request', {id:1, name: "John"});