Giter Site home page Giter Site logo

onMessage hook? about ws-wrapper HOT 6 CLOSED

bminer avatar bminer commented on May 31, 2024
onMessage hook?

from ws-wrapper.

Comments (6)

bminer avatar bminer commented on May 31, 2024 1

Also with ws-server-wrapper, you can easily count the total number of incoming messages (not per socket) using server.on("message", listener). In this case listener will be called for each inbound message. Within the listener itself, this would refer to the wrapped socket, I believe.

from ws-wrapper.

bminer avatar bminer commented on May 31, 2024 1

I've contemplated supporting multiple event listeners for a given event (each running in order), which would support the middleware concept a little bit. I've also considered adding a special event name called * (asterisk) whose event listeners would run on every inbound event / request -- this could be channel specific or global.

At any rate, I'm glad you got things sorted on your end. At point in the future, I'm sure I will revisit this topic because I use this lib for quite a few projects.

from ws-wrapper.

bminer avatar bminer commented on May 31, 2024

This is a great question. I'll look into it...

from ws-wrapper.

bminer avatar bminer commented on May 31, 2024

@daniandl - Whenever a message is received, the "message" event is fired, as you can see here: https://github.com/bminer/ws-wrapper/blob/master/lib/wrapper.js#L87-L91

But the question remains: how do you implement rate limiting? This might be a case where ws-wrapper should provide the functionality out of the box...

I don't know. Do you have any other thoughts?

from ws-wrapper.

bminer avatar bminer commented on May 31, 2024

One idea... use the "message" event to count inbound messages per connection. If the message count (or total byte count) is too high for a given window, you just call disconnect() on the socket. You can get more sophisticated by keeping track of offending IP addresses in a database or whatever and randomly dropping these connections before HTTP upgrade.

Anyway, please feel free to post back here, but I'm going to close for now. Thanks!

from ws-wrapper.

daniandl avatar daniandl commented on May 31, 2024

Hi @bminer, thanks for looking into this so quickly :)

Yes, I'm aware that it's possible to write rate limit logic to flag clients, but it's tedious to then have to check if(isRateLimited) on every socket.on server-side :D

I spent some time today making my own implementation, and I've found a way to make it work without needing to fork this repo, nor ws-server-wrapper:

onConnection

const oldHandler = socket._onMessage
socket._onMessage = newHandler.bind(socket, oldHandler)

newHandler(function)

function newHandler(oldHandler, data) {
  const socket = this  // quality of life

  // i apply "rateLimited" to the socket in my rate limiting system, not relevant here
  if (socket.get('rateLimited')) {
    const msg = JSON.parse(data)
    
    // only reply if its a socket.request
    if (msg && msg.i) socket._sendReject(msg.i, new Error(Errors.RateLimited))

    return // stop here and prevent calling the oldHandler
  }

  oldHandler.call(socket, data)  // proceed to call the actual _onMessage from your lib
}

This only really works because this is a hook that specifically needs to run before one of your internal functions. If this hook was afterwards, I'd probably have to copy paste your entire _onMessage function. But this works out nicely for me, so I count it as a success 😃

If you're looking to extend the framework, I guess you could allow people to pass a beforeOnMessage options (function) to the constructor and then insert the hooks into your various internal class methods. Then people can write their own custom logic and call a next() function when they're done? Just my two cents 👍

from ws-wrapper.

Related Issues (14)

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.