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

socketio-mq

v0.0.3

Published

A simple message queue using socket.io (typescript support)

Downloads

4

Readme

socketio-mq

Hits Forks Stargazers

Overview

socketio-mq is a library that combines typing with Socket.IO and message queues to provide a structured and efficient way of handling real-time communication and event-driven architecture. It allows you to define typed events and messages, ensuring type safety and improving the development experience.

Features

  • Typed events and messages: Define events and messages with specific types, enabling type checking and autocompletion.
  • Integration with Socket.IO: Seamlessly integrate with Socket.IO for real-time communication between clients and servers.
  • TypeScript support: Written in TypeScript, providing type safety and enhanced developer productivity.

Installation

With npm:

npm install socketio-mq

With yarn:

yarn add socketio-mq

Usage

StaticClient

Use StaticClient when you want to build something in class and use OOP features like inheritance

import { RemoteHandler, StaticClient, Server } from "socketio-mq"

const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))

class ServiceA extends StaticClient {
 static id = "service-a"
 static url = "http://localhost:3000"

 // Use @RemoteHandler for register method as "remote-able" or else these methods will be recognize as not "remote-able" and throw error if trying to use remote
 @RemoteHandler
 async getPosts(userID: number) {
  await delay(1000)
  return ["post1", "post2", "post3"]
 }
}

class ServiceB extends StaticClient {
 @RemoteHandler
 async getUser(id: number) {
  await delay(1000)
  return { id, name: "John Doe" }
 }
}

const server = new Server(3000)

const serviceA = ServiceA.getInstance() // Singleton support (ip and url is defined in class)
const serviceB = new ServiceB("service-b", "http://localhost:3000") // Construct new instance (will override "ip" or "url" if you specific in constructor params)

const remoteB = serviceA.use(ServiceB, "service-b") // Create remote service B

;(async () => {
 const user = await remoteB.getUser(1) // remote call
 const post = await serviceA.getPosts(1) // normal call

 console.log(`user: ${JSON.stringify(user)}, post: ${post}`)
})()

DynamicClient

Use DynamicClient when you want to build something flexible, register handler anywhere, anytime

import { DynamicClient, Server } from "socketio-mq"

const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))

// Define events map first

type EventMapA = {
 getPosts: (userID: number) => Promise<string[]>
}

type EventMapB = {
 getUser: (id: number) => Promise<{ id: number; name: string }>
}

const server = new Server(3000)
const serviceA = new DynamicClient<EventMapA>(
 "service-a",
 "http://localhost:3000"
)
const serviceB = new DynamicClient<EventMapB>(
 "service-b",
 "http://localhost:3000"
)

serviceB.on("getUser", async (id: number) => {
 await delay(1000)
 return { id, name: "John Doe" }
})

// We will not register handler for "getPosts" here to see error!
// a.on("getPosts", async (userID: number) => {
//  await delay(1000)
//  return ["post1", "post2", "post3"]
// })
;(async () => {
 const remoteB = serviceA.use<EventMapB>("service-b")

 const user = await remoteB.getUser(1)
 const post = await serviceA.useSelf().getPosts(1)
 console.log(`user ${JSON.stringify(user)}, post: ${post}`)
})().catch((e) => {
 console.log("We got an error: ", e)
 // Error: Client "service-a" does not have a handler for event "getPosts". Make sure to call the "on" method to register the handler!
})

Server

Just a socket.io server for the clients

import { Server } from "socketio-mq"
const server = new Server(3000) // Socket.io server will lift at http://localhost:3000

Conclusion

This package is "message queue" but still has lacks of features to be a full-featured message queue. It's more like a "message broker" for now, would be the best if you guys can help me to improve this package. Thanks! 🙏