I need to inform all users about adding a new Record database . So I have the following code
Application.java - here I posted a socket handler method
public WebSocket<JsonNode> sockHandler() {
return WebSocket.withActor(ResponseActor::props);
}
Then i opened the connection
$(function() {
var WS = window['MozWebSocket'] ? MozWebSocket : WebSocket
var socket = new WS("@routes.Application.sockHandler().webSocketURL(request)")
socket.onmessage = function(event) {
console.log(event);
console.log(event.data);
console.log(event.responseJSON)
}});
Class of my actor
public class ResponseActor extends UntypedActor {
private final ActorRef out;
public ResponseActor(ActorRef out) {
this.out = out;
}
public static Props props(ActorRef out) {
return Props.create(ResponseActor.class, out);
}
@Override
public void onReceive(Object response) throws Exception {
out.tell(Json.toJson(response), self());
}
}
And the last, it seems to me, I need to call the Actor from my response controller
public Result addPost() {
Map<String, String[]> request = request().body().asFormUrlEncoded();
Response response = new Response(request);
Map<String, String> validationMap = ResponseValidator.validate(response.responses);
if (validationMap.isEmpty()) {
ResponseDAO.create(response);
ActorRef responseActorRef = Akka.system().actorOf(ResponseActor.props(outRef));
responseActorRef.tell(response, ActorRef.noSender());
return ok();
} else {
return badRequest(Json.toJson(validationMap));
}
}
My question is: what is it ActorRefand where can I get it in my controller? Could you explain the logic of sending updates to all clients via web sockets?
source
share