Stateful Session Management



in my project I want to have a stateful service, that would receive a stream of commands from a Client. For each Client, the service holds an integer value. By using the request stream the client should be able to control the integer value (lets say to add or distract some integer values) and then the service would emit the up-to-date value back to the user.

I had a look at the code presented at: How to create Stateful Functions using RSocket RPC service?

There the session of the client is not managed. How do I manage the session. Is there a better solution than having a map of session IDs to manage the clients?



Right now it would depend how you routed your requests. If you used “group” routing it will load balance across servers so there wouldn’t be a way to keep state. If you tag based routing you could route to servers based on a tag and then use that to keep state. We are going to be adding more support for sessions in the future.

We can create a demo that shows how to do this with the current release.




a demo of tag routing would be really amazing. And another question. Like if in my architecture I have several service instances, connected to proteus. Now a client would establish a stream channel with the proteus. Would the proteus then establish stram connection to a single service instance? Or would it instead load-balance the client stream between the service instances?

Thank you so much for your help :slight_smile:



Since there was multiple people interested in this I created a more sophisticated example than I had original planned. Some of the functionality will be folded into the broker / broker client to make this transparent in the future.

I created a class called StatefulSocket. This class creates a session service using the DefaultSessionService service. The StatefulSocket creates a soft sticky connection to a particular service that is valid as long as there is an active RSocket connection. It uses an API available in the Netifi Broker that will notify you when a destination leaves. If either the client or service are discounted, or the StatefulSocket is closed the session is closed. Each StatefulSession has a sessionId that is unique, and be used to tie items in the session. The StatefulSocket acts like a normal RSocket and you can pass it to RSocket-RPC client etc. All requests from the StateSocket will go to the destination that it’s tied with for the socket’s lifetime.

To get the demo to work right now you need to build the netifi-java develop branch locally. Here’s the branch:

We’ve been moving some things around with our builds - I will send information to a repository with a snapshot when it’s available. Here’s a link to the to the example:

Let me know if you have any questions.


1 Like
How to create a Session with Transaction using RSocket-RPC?

hi @robertroeser I hope you are doing well.
I tried today to test the stateful session but unfortunately I wasn’t capable to build the netifi-java because I need to have your netifyReadOnlyUsername and password.
Is it possible for you to share this information?
Another question is about the pure rsocket when do you think the stateful session will be available ?
Thanks a lot for your help.
Best regards,


Hi @belkirdi,

We have release planned for Monday (tomorrow) that will have the changes needed make the sample code work.