Comments (8)
I think it's better to use a middleware if the validation should be done before the 'connection'
event is emitted.
from primus.
I think it's better to use a middleware if the validation should be done before the
'connection'
event is emitted.
Hi,
Yes, I looked at middleware and tried to implement that but I had some problems with that.
What I tried looked something like this:
this.primus.use(() => {
return async (req, res) => {
if(await doStuff()) {
req.socket.destroy();
return;
}
}
});
And it worked as I wanted to (connection was closed if wrong params were provided) but the client kept creating new connections and I couldn't find out why.
That's why I tried with plugin where I have access to spark and it worked in the way I described in the issue.
Is there a way I use middleware and tell the client to not open new connections?
from primus.
One issue I see in the above example is that Primus does not support middleware that return a promise. Asynchronous middleware take an additional next
argument that should be called. You could do something like this
primus.use(() => {
return async (req, res, next) => {
let ret;
try {
ret = await doStuff();
} catch(e) {
res.destroy();
return;
}
if (ret) {
res.destroy();
return;
}
next();
}
});
from primus.
One issue I see in the above example is that Primus does not support middleware that return a promise. Asynchronous middleware take an additional
next
argument that should be called. You could do something like thisprimus.use(() => { return async (req, res, next) => { let ret; try { ret = await doStuff(); } catch(e) { res.destroy(); return; } if (ret) { res.destroy(); return; } next(); } });
I tried it but the issue is that I'm using TypeScript and definition in the declaration file does not allow addional next parameter as it looks this:
type Middleware = (req: http.IncomingMessage, res: http.ServerResponse) => void;
And use function looks like this:
use(fn: () => Middleware | Middleware, options?: object, level?: number): this;
Aynyway I overwrote it and I tried something like this:
this.primus.use(() => {
return async (req, res, next) => {
try {
if (!await doStuff()) {
res.destroy();
return;
}
} catch (error) {
res.destroy();
return;
}
next();
}
});
Unfortunately it still didn't work so most likely I'll have to stick to calling the plugin directly in the 'connection'
handler. Unless there's another solution.
from primus.
What is the issue? The client keeps connecting when "validation" fails? If so it might be a reconnection issue. The client does not know if the disconnection was caused by a validation failure. In that case you might want to disable automatic reconnection.
from primus.
What is the issue? The client keeps connecting when "validation" fails? If so it might be a reconnection issue. The client does not know if the disconnection was caused by a validation failure. In that case you might want to disable automatic reconnection.
Yes, this is exactly the issue and I think that you're right about the reconnection. The issue is that I'm not sure if I can disable it but I would say that we can close this issue with that conclusion. Thank you very much for your help!
from primus.
You can try to remove the 'timeout'
strategy and see it makes any difference. See https://github.com/primus/primus#strategy.
See also https://github.com/primus/mirage for an example of an "async" plugin but I don't think it makes much sense in your case.
from primus.
You can try to remove the
'timeout'
strategy and see it makes any difference. See https://github.com/primus/primus#strategy.See also https://github.com/primus/mirage for an example of an "async" plugin but I don't think it makes much sense in your case.
Yeah, I tried removing the strategy after I closed the issue and it worked like I wanted to but unfortunately we need some reconnection strategy in our app so this is not applicable in my case.
I'll have look at the async plugin example you referenced, thank you.
from primus.
Related Issues (20)
- update documentation to be more explicit around createSocket
- Problem using proxy with primus client side
- Nativescript support HOT 1
- Does the primus web client work with a non-primus websocket server? HOT 4
- How can I know if a connection has been upgraded or not? HOT 4
- import primus client in client JS
- Getting response headers while polling HOT 3
- public connections and connected HOT 1
- Consider to rewrite without prototype pollution? HOT 11
- Primus with Koa and websockets transport HOT 3
- Can you please consider changing "Primus, the creator god of transformers" HOT 3
- primus doesn't accept custom path with engine.io HOT 10
- support of the new uwebsocket version
- Allow Primus instance to select version of transformer/engine library
- Server Side Events (SSE) source HOT 7
- Maximum Concurrent Connection on Primus Server HOT 2
- Consider alternative to deprecated navigator.userAgent getter. HOT 1
- __name is not defined HOT 1
- Primus breaks Esc to close <dialog>
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 primus.