Git Product home page Git Product logo

hemera-nats-streaming's People

Contributors

marcoreni avatar starptech avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

marcoreni

hemera-nats-streaming's Issues

Is this plugin production ready - how can it work with multiple services?

Sorry if this post comes out rude in any way, but we tried to use this plugin in our upcoming project (given that it's mentioned in the Hemera documentation) and it turned into hours of trying to make sense of the things that we were seeing in logs. When I finally went looking into the source code I was surprised to say the least, and still have no idea how it's supposed to be working with more than one service. I'll give an example to illustrate what I mean.

From what I gather, this plugin more or less works in the following way (please correct me if I'm wrong at any point):
For publishing:

  • On connect, add a Hemera handler on natss topic with publish command
  • In this handler, emit a NATS streaming event with a subject passed as a parameter

For subscriptions:

  • Create a NATS streaming subscription when hemera.natss.add method is used
  • When an event is received from NATS streaming, execute Hemera.act with natss.event-subject topic from a subscription handler
  • Events can be listened by adding a Hemera handler on natss.subject topic

Now, let's imagine that we have 3 independent services, all using Hemera with Hemera-nats-streaming plugin. Let's call them A, B and C. An event with a subject user-created is published by A, while B and C wish to subscribe to it.

  1. On B and C we create a NATS steaming subscription via
hemera.natss.add({
  subject: 'user-created'
})

and then add a handler via Hemera

hemera.add({ topic: 'natss.user-created' }, async () => { })
  1. On A, we publish the message with
hemera.act({
  topic: 'natss',
  cmd: 'publish',
  subject: 'user-created',
  data: { }
})

At this point strange things start to happen:

  1. Random service (A, B or C) handles the Hemera call and emits the actual event to NATS streaming

I don't know why events are first passed through NATS with Hemera before being published in NATS streaming (is this a Hemera plugin limitation?), but since all services have handler on the same natss topic, it gets routed to a randomly selected one, instead of the one that tries to emit an event in a first place.

At this point it still works correctly, even though the network traffic is doubled and it's very confusing to debug (for example, event handler error logs end up in random services).

  1. B and C both receive the event inside the NATS streaming subscription in the plugin

Looking good at this point, every service received the event just as intended

  1. From a plugin, B and C both make a Hemera request that contains the event with natss.user-created topic

At this point all hell breaks loose. We have two services listening on a topic natss.user-created. This means that any of the following scenarios can happen, at random:

  • A receives the event twice, B never gets it
  • B receives the event twice, A never gets it
  • Both A and B get the event once

You can imagine how difficult it was to debug, given that it works correctly (though not efficiently) with a single listener for a given event, but breaks as soon as you add a second one and only get worse with more.

It seems less like a bug and more like a flaw in a way that a plugin is designed, so I don't know what the solution should be here. Things that comes to my mind are:

  • if we have completely misused this plugin somehow, please make it clear in the documentation on how it should be used with more than one service
  • if it's not meant to be used in production, please make it explicit in the Hemera documentation

If it's possible to intercept Hemera in a way that would allow passing events in memory (both when publishing and subscribing) I think it would resolve most of the issues, but I don't know if Hemera allows it in a first place.

How to shutdown?

Hello,

I'm trying to perform some tests using Jest.

Jest returned an error about open Handlers, and after some debugging it seems that a nats connection is left open.

I tried to check if there was a way to close nats connection inside hemera-nats-streaming, but it seems that the onClose handler does nothing:
https://github.com/hemerajs/hemera-nats-streaming/blob/master/index.js#L35

Shouldn't this function close the connection to nats server? Is there any other way to gracefully shutdown the connection?

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.