Damian
Damian

Reputation: 51

Akka Websocket server push in java

I have a Websocket implementation in Akka where a client can send a simple request and the server can reply, but I would like to know how can I spontaneously send a message from a server to the client in Akka?

Upvotes: 3

Views: 877

Answers (1)

Damian
Damian

Reputation: 51

I figured out how to do that:

First, you need to do an ActorPublisher to push the messages to the client.

Second, you need to create a Flow which uses Source.actorPublisher(Props.create(yourActorPublisher.class)), you will pass the created flow to the handleWebSocketMessages route.

Now to push messages to the client, you just need the ActorRef of the Publisher and push message by yourActorRef.tell(yourMessage,ActorRef.noSender())

Upvotes: 1

Related Questions