ngx-mqtt icon indicating copy to clipboard operation
ngx-mqtt copied to clipboard

Multiple connections

Open woodrunner opened this issue 5 years ago • 11 comments

  • [X] I have searched for similar issues in this repository, but couldn't find one.
  • [X] I have read the README and have a basic understanding how angular works.

I'm submitting a...

  • [ ] Regression (a behavior that used to work and stopped working in a new release)
  • [ ] Bug report  
  • [X] Feature request

Current behavior

Is it currently possible to connect to multiple brokers in the same Angular app? Everytime I open a 2nd connection with a different hostname or port or query params, the old websocket gets closed.

Expected behavior

Connecting to multiple websocket brokers.

Environment


ngx-mqtt version: 6.13.2

woodrunner avatar Jan 03 '20 15:01 woodrunner

Yes, you could use a factory for creating the service. the service and the underlying mqtt.js client instance uses one connection.

Adapting the code to support multiple connections/clients In the current service architecture would lead to many questions like which subscription should be made in which client and which publication publishes to which client and so on.

Could you elaborate how you want to use multiple connections to different brokers?

sclausen avatar Jan 03 '20 19:01 sclausen

Thank you for your answer. I will try your solution!

Because we have a high availability setup for our platform, we stumble across certain issues where it comes down to losing messages if we only have one WebSocket open in one zone. Therefore, one WebSocket has to be set up for each zone so that both zones have their own WebSocket.

woodrunner avatar Jan 06 '20 06:01 woodrunner

I don‘t think exposing high availability nodes to endpoints is a good idea, except you really don’t care about duplicate messages. A mqtt cluster solution for example is provided by HiveMQ, but your last comment sounded like the tech stack might be specified.

For your special requirements I actually would suggest changing the code so you can add multiple connections and subscribe and publish to all at once. I think this should not be incorporated in the master of this project, but it might be a solution for your issue.

sclausen avatar Jan 15 '20 00:01 sclausen

This would be very cool, I have also the requirement to connect to multiple brokers.

Regards Cédric

cedricve avatar Nov 10 '20 16:11 cedricve

@sclausen is there any example that we could to this repo, for example in the examples folder?

cedricve avatar Nov 10 '20 17:11 cedricve

An example for usage is here or what do you mean?

sclausen avatar Nov 10 '20 17:11 sclausen

Sorry the comment was related to the one above. Illustrating the factory patttern.

cedricve avatar Nov 10 '20 18:11 cedricve

I posted a link to stackoverflow on an example how to use the factory pattern in the comment above

sclausen avatar Nov 10 '20 18:11 sclausen

yes I know, and it works, I was just suggesting for upcoming users of your library, it might be good to document it inside the repo. So they do not end searching in the issue list (as what I've been doing).

From my opinion it's overkill to implement this feature into the source of your library. The dependency injection is good enough, but should be documented..

cedricve avatar Nov 10 '20 19:11 cedricve

I think the new setup with @angular/cli encourages examples inside a project. I'll put something together. Any wishes? @nosovk @AlxZchk @cedricve

sclausen avatar Nov 10 '20 21:11 sclausen

Probably we can use https://www.emqx.io/mqtt/public-mqtt5-broker to avoid installing local broker for testing

nosovk avatar Nov 11 '20 18:11 nosovk