We have the following setup.
A separate frontend (Angular) and backend (Spring boot with Axon framework) application. Our frontend will be deployed as a single service while the backend has multiple instances (All connected to Axon Server) of the same service (loadbalanced). The communication between front- and backend is done using websockets (Atmosphere).
Now we’re struggling with the following use case. The client (frontend) will open a websocket at let’s say wss://…/query/users and it sends a FindAllUsersQuery object to the server. The server will subscribe to that query using the subscriptionQuery on the queryGateway.
e.g
SubscriptionQueryResult<..> fetchQueryResult = queryGateway.subscriptionQuery(findAllUsersQuery, ...);
Next, the server will fetch the initial result and send it back over the /query/users socket.
e.g.
return fetchQueryResult.initialResult().block.stream();
All subsequent updates on that query (triggered by the QueryUpdateEmitter), will be sent from the server over the same socket.
e.g.
fetchQueryResult.updates().subscribe( users -> {
metaBroadcaster.broadcastTo("/query/users", users); // atmosphere websocket broadcast to all connections on /query/users
})
So far, no problem.
However, each time a new socket is opened - either by the same user (eg when opening a new tab in the browser) or someone else - for the same Query, a new subscriptionQuery is registered, resulting in broadcasting the result as many times as there are subscriptions (5 subscriptions → 5 query callbacks → 5 messages to the same user). Moreover, even if we can somehow realize that only 1 subscription for a specific query is active on a specific instance, we’d still don’t know if someone else wants to register the same subscription query on another instance (after all, we halve multiple instances running of the same application).
So how do we make sure that we only have one subscription running for a specific Query on multiple instances of our service? Or isn’t this the way subscriptionQueries are intended to be used?
And would the giftcard example (where you use vert.x and Vaadin) still work when deployed X times? Wouldn’t it suffer from the same problem that we mention here above? And how do we make sure that the number of subscription queries doesn’t explode (number of threads, potential leaking of queries that are never cancelled/closed, etc) ?