João
João

Reputation: 165

Send message to client any moment Netty Io

I have an architecture based on micro services, netty io runs on a spring boot application, when I want to send a message to a client I have to wait for a message from the client to send the reply to the one I want to send. How could I send a message to the client without waiting for him to communicate with me? I'm lost in this step.

Upvotes: 3

Views: 3334

Answers (1)

Prim
Prim

Reputation: 2968

You can process like that :

  1. Let the client connect on your server
  2. Let the client send a Hello Message (to be sure is one of your client)
  3. Catch the message on the server and store a reference on the channel (which is available on each channel handler by calling ctx.channel())
  4. Catch channelInactive event to remove the reference when the client disconnects

Instead of waiting for a HelloMessage, you can also catch the channelActive event but it is fired each time somebody connects to your listened port, even if is not one of your client. That's why I prefer using an "Hello Message"

Now, you have a reference on the channel and you can send a message when you want by calling channel.writeAndFlush()

Upvotes: 7

Related Questions