Giter Club home page Giter Club logo

Comments (8)

lpinca avatar lpinca commented on June 28, 2024

I think it's better to use a middleware if the validation should be done before the 'connection' event is emitted.

from primus.

MateuszKikmunter avatar MateuszKikmunter commented on June 28, 2024

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.

lpinca avatar lpinca commented on June 28, 2024

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.

MateuszKikmunter avatar MateuszKikmunter commented on June 28, 2024

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();
  }
});

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.

lpinca avatar lpinca commented on June 28, 2024

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.

MateuszKikmunter avatar MateuszKikmunter commented on June 28, 2024

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.

lpinca avatar lpinca commented on June 28, 2024

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.

MateuszKikmunter avatar MateuszKikmunter commented on June 28, 2024

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)

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.