GithubHelp home page GithubHelp logo

Comments (1)

MegaGM avatar MegaGM commented on May 23, 2024

Apologies for the delayed response. Of course it's possible.
Here I made a very simplified example of how you could implement message aggregation using middlewares.

// client
!async function doSomethingWithFlushedBuffer() {
  const channel = socket.subscribe('RushikeshWatchingList')
  for await (const data of channel) {
    console.info('RushikeshWatchingList data', data)
  }
}()
// server
const messageBuffer = {}
const bufferableChannels = ['currency1', 'currency2', 'currency3']

agServer.setMiddleware(agServer.MIDDLEWARE_OUTBOUND, async (middlewareStream) => {
  for await (const action of middlewareStream) {
    if (action.type === action.PUBLISH_OUT) {
      const { socket, channel, data } = action

      if (!bufferableChannels.includes(channel)) {
        action.allow()
        continue
      }

      if (!messageBuffer[socket.id]) {
        messageBuffer[socket.id] = []
      }
      messageBuffer[socket.id].push({ channel, data })
      action.block(`Message has been buffered`)

      if (messageBuffer[socket.id].length <= 500) {
        continue
      }

      agServer.exchange.transmitPublish('RushikeshWatchingList', messageBuffer[socket.id])
      delete messageBuffer[socket.id]
      // Message buffer has been flushed
    }
  }
})

If you have some other questions, please, ask them in chat https://gitter.im/SocketCluster/socketcluster

from socketcluster.

Related Issues (20)

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.