Recording Connection & Disconnection events. #6445
-
OverviewWe are building a real-time communication system based on NATS, where the clients will connect to the 3-node NATS cluster hosted on AWS. The NATS server version is ProblemHowever, we have few additional requirements as below: We need to keep track of which client is connected, and which client is disconnected. We initially planned to use NATS System Events on the backend side to keep track of this. The plan was to listen on
Due to this, we changed the approach for connection event and mark the client as connected whenever we consume a message from the stream. However, this approach does not work for the disconnection event, as the client can become disconnected due to any arbitrary cause such as power loss, internet connection loss, etc. The business requirement also requires us to filter the online/offline clients, that is why we store this information in the database along with some other client metadata. Otherwise, if the requirement was just to identify the online/offline clients, we could look at the NATS consumer (with small Inactive Threshold) to identify the client online/offline status. But this requirement makes it difficult to use this approach. QuestionsTo solve the above problems, we are thinking about the following questions, which can help us in further design.
If there is any additional information that is needed, please let me know. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
I realized that this can be done with NATS KV store where the clients can put some value with its unique key, and it will be expired after the set TTL. However, after checking I came to know that TTL'd messages do not get reported to watchers, yet. Is that going to be released in 2.11 release? |
Beta Was this translation helpful? Give feedback.
-
Re. your original question, you can export the $SYS.ACCOUNT.*.CONNECT/DISCONNECT evens to another account, and create a stream there to capture them. Set up the stream with --no-ack as there is no point on confirming the capture. |
Beta Was this translation helpful? Give feedback.
-
The KV option is great, as long as the clients are willing to collaborate :-) |
Beta Was this translation helpful? Give feedback.
-
You can also send a request to all servers to collect info on all the connections ($SYS.REQ.SERVER.PING.CONNZ) including closed ones (send payload with '{"state":1}'). But that may not be realistic in your case, with over 100k active connections! |
Beta Was this translation helpful? Give feedback.
Yes correct and this has landed on main.