1.0.1 • Published 1 year ago

@ferdinandvonhagen/socket-io-aws-sqs-emitter v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

The Socket.IO AWS SQS emitter

Build Status NPM version

The @socket.io/aws-sqs-emitter package allows you to easily communicate with a group of Socket.IO servers from another Node.js process (server-side).

Table of content

How to use

Installation:

npm i @socket.io/aws-sqs-emitter @aws-sdk/client-sns

CommonJS

const { Emitter } = require("@socket.io/aws-sqs-emitter");

const snsClient = new SNS();

redisClient.connect().then(() => {
  const io = new Emitter(snsClient, "arn:aws:sns:eu-central-1:000000000000:socket-io");

  setInterval(() => {
    io.emit("time", new Date);
  }, 5000);
})

TypeScript

import { Emitter } from "@socket.io/aws-sqs-emitter";

const snsClient = new SNS();

redisClient.connect().then(() => {
  const io = new Emitter(snsClient, "arn:aws:sns:eu-central-1:000000000000:socket-io");

  setInterval(() => {
    io.emit("time", new Date);
  }, 5000);
});

With typed events:

import { Emitter } from "@socket.io/aws-sqs-emitter";

const snsClient = new SNS();

interface Events {
  basicEmit: (a: number, b: string, c: number[]) => void;
}

redisClient.connect().then(() => {
  const io = new Emitter<Events>(snsClient, "arn:aws:sns:eu-central-1:000000000000:socket-io");

  io.emit("basicEmit", 1, "2", [3]);
});

Emit cheatsheet

import { Emitter } from "@socket.io/aws-sqs-emitter";

const snsClient = new SNS();
const io = new Emitter(snsClient, "arn:aws:sns:eu-central-1:000000000000:socket-io");

// sending to all clients
io.emit(/* ... */);

// sending to all clients in 'room1' room
io.to("room1").emit(/* ... */);

// sending to all clients in 'room1' except those in 'room2'
io.to("room1").except("room2").emit(/* ... */);

// sending to individual socketid (private message)
io.to(socketId).emit(/* ... */);

const nsp = io.of("/admin");

// sending to all clients in 'admin' namespace
nsp.emit(/* ... */);

// sending to all clients in 'admin' namespace and in 'notifications' room
nsp.to("notifications").emit(/* ... */);

Note: acknowledgements are not supported

License

MIT