Coder Social home page Coder Social logo

khoakomlem / socketio-mq Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 64 KB

A message broker using Socket.IO and Typescript

Home Page: https://www.npmjs.com/package/socketio-mq

License: MIT License

JavaScript 7.64% TypeScript 92.36%
message-queue realtime rpc socket-io message-broker

socketio-mq's Introduction

socketio-mq

Hits Forks Stargazers


Logo

SOCKET.IO MQ

An integration of typing with Socket.IO and message queue

View Demo · Report Bug · Request Feature

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! 🙏

socketio-mq's People

Contributors

khoakomlem avatar

Stargazers

Lê Quốc Bình avatar toanehihi avatar Minh Triet Nguyen avatar Lê Đức Toàn avatar

Watchers

 avatar

socketio-mq's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.