Best way to handle connection object in NATS? #654
-
Is it better to create a long lived connection instance to the NATS broker, or ignore creating the connection as internally publish method is creating connection. Also in case of consumer, does the library internally handle connection to broker if its not connected already? Am i getting a performance improvement if i create a long lived connection to broker or is it actually degrading my performance by consuming memory ? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Yes, typically you should create a connection once as a singleton and use the same object until your application exists. Library internally maintains the connection, reconnects after connection is lost and maintains subscriptions and consume calls. |
Beta Was this translation helpful? Give feedback.
that's correct. you'd want the connection to be a singleton so it's established and kept alive internally (by reconnecting when needed)