Comments (6)
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.
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.
This is a great question. I'll look into it...
from ws-wrapper.
@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.
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.
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)
- Channel does not have `get()` or `set()` methods
- Type definitions HOT 3
- Hooks HOT 9
- Option to disable pendingSend globally HOT 14
- Export ES Modules HOT 2
- Server fails with error after client emitted event HOT 10
- Change constructor property names to avoid weird issues
- node app as client HOT 1
- Compile to ES5 HOT 1
- Maybe emit "connect" event in addition to "open" event? HOT 1
- whos better this or socket io? HOT 9
- "a" leading sendEvent data HOT 3
- Protocol mismatch: packets are sent as { "a": { "0": "connect" }} instead of { "a": ["connect"] } HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ws-wrapper.