Using Actors to send data to websockets

94 views
Skip to first unread message

Anmol Singh Jaggi

unread,
Oct 6, 2017, 7:21:54 AM10/6/17
to Akka User List
I have been experimenting with Akka websockets and was able to send some static data to a websocket using the following code:

    import java.io.BufferedReader;
   
import java.io.InputStreamReader;
   
import java.util.concurrent.CompletionStage;
   
import java.util.concurrent.TimeUnit;


   
import akka.NotUsed;
   
import akka.actor.ActorSystem;
   
import akka.http.javadsl.ConnectHttp;
   
import akka.http.javadsl.Http;
   
import akka.http.javadsl.ServerBinding;
   
import akka.http.javadsl.model.HttpRequest;
   
import akka.http.javadsl.model.HttpResponse;
   
import akka.http.javadsl.model.ws.Message;
   
import akka.http.javadsl.model.ws.WebSocket;
   
import akka.japi.Function;
   
import akka.stream.ActorMaterializer;
   
import akka.stream.Materializer;
   
import akka.stream.javadsl.Flow;
   
import akka.stream.javadsl.Sink;
   
import akka.stream.javadsl.Source;


   
public class Server {


     
public static HttpResponse handleRequest(HttpRequest request) {
       
System.out.println("Handling request to " + request.getUri());
       
if (request.getUri().path().equals("/greeter")) {
         
final Flow<Message, Message, NotUsed> greeterFlow = greeterHello();
         
return WebSocket.handleWebSocketRequestWith(request, greeterFlow);
       
} else {
         
return HttpResponse.create().withStatus(404);
       
}
     
}


     
public static void main(String[] args) throws Exception {
       
ActorSystem system = ActorSystem.create();


       
try {
         
final Materializer materializer = ActorMaterializer.create(system);


         
final Function<HttpRequest, HttpResponse> handler = request -> handleRequest(request);
         
CompletionStage<ServerBinding> serverBindingFuture = Http.get(system).bindAndHandleSync(handler,
             
ConnectHttp.toHost("localhost", 8080), materializer);


         
// will throw if binding fails
          serverBindingFuture
.toCompletableFuture().get(1, TimeUnit.SECONDS);
         
System.out.println("Press ENTER to stop.");
         
new BufferedReader(new InputStreamReader(System.in)).readLine();
       
} finally {
          system
.terminate();
       
}
     
}


     
public static Flow<Message, Message, NotUsed> greeterHello() {
       
return Flow.fromSinkAndSource(Sink.ignore(),
           
Source.single(new akka.http.scaladsl.model.ws.TextMessage.Strict("Hello!")));
     
}
   
}


I now want to send some data to a websocket from an Actor, something like this:  

    import akka.actor.ActorRef;
   
import akka.actor.UntypedActor;


   
public class PushActor extends UntypedActor {
     
@Override
     
public void onReceive(Object message) {
       
if (message instanceof String) {
         
String statusChangeMessage = (String) message;
         
// How to push this message to a socket ??
       
} else {
         
System.out.println(String.format("'%s':\nReceived unknown message '%s'!", selfActorPath, message));
       
}
     
}
     
   
}

I am unable to find any example of how to do this online.
Could somebody please guide me on this?

Rob Crawford

unread,
Oct 6, 2017, 9:12:58 AM10/6/17
to Akka User List

Anmol Singh Jaggi

unread,
Oct 6, 2017, 1:38:43 PM10/6/17
to Akka User List
Yes, but it wasn't very helpful.

Mahesh Venugopal

unread,
Mar 17, 2020, 5:14:44 AM3/17/20
to Akka User List
Hi Anmol,

I am currently looking for an answer to the exact same question that you posted here. I still haven't been able to find an answer to this. Had you been successful in finding an answer to this. Any help is greatly appreciated.
Reply all
Reply to author
Forward
0 new messages