Hi Alan,

I hope the Akka/Java example has helped, I will eventually migrate to it 
when Akka HTTP websockets performance gets better compared to Vert.x with 
Netty implementation:

https://www.techempower.com/benchmarks/#section=data-r12&hw=peak&test=json

If you notice on my original source code (in case you still need the 
answer), I'm passing the upgraded socket to the actor so it should just be 
a final property of the newly created actor.
Such socket has a write method, I'll post here again another -and working- 
example with two paths, notice that for each path a different type of actor 
is created and the upgraded socket is part of the actor creator:


      vertx.createHttpServer().requestHandler(request -> {
        switch (request.path()) {
          case "/price": {
            final ServerWebSocket socket = request.upgrade();
            final ActorRef actorRef = context.actorOf(Props.create(new 
PriceWebsocketCreator(TargetSupervisor.this, socket)));
            socket.setWriteQueueMaxSize(1024).
              handler(event -> actorRef.tell(event, NO_SENDER)).
              closeHandler(event -> actorRef.tell(PoisonPill.getInstance(), 
NO_SENDER));
            log.info("Price websocket connection from '{}' to '{}' 
established.", socket.remoteAddress(), socket.localAddress());
            break;
          }
          case "/ticket": {
            final ServerWebSocket socket = request.upgrade();
            final ActorRef actorRef = context.actorOf(Props.create(new 
TicketWebsocketCreator(TargetSupervisor.this, socket)));
            socket.setWriteQueueMaxSize(1024).
              handler(event -> actorRef.tell(event, NO_SENDER)).
              closeHandler(event -> actorRef.tell(PoisonPill.getInstance(), 
NO_SENDER));
            log.info("Ticket websocket connection from '{}' to '{}' 
established.", socket.remoteAddress(), socket.localAddress());
            break;
          }
          default:
            request.response().setStatusCode(400).end();
        }
      }).listen(config.getInt("http.port"), config.getString("http.host"));


HTH,

Guido.

On Tuesday, March 8, 2016 at 10:03:23 AM UTC, Alan Klikic wrote:
>
> Hi Guido,
>
> this post helped me allot. Thanks.
> How can I send message from the Actor to the "connected" websocket?
> As a response to initial message received from websocket and as a 
> standalone/push message from Actor to websocket?
>
> Thank you in advance.
>
> Br,
> Alan
>
> Dana srijeda, 24. veljače 2016. u 13:36:17 UTC+1, korisnik Guido Medina 
> napisao je:
>>
>> While Akka HTTP is accessible to Java 8 users I decided to go for an 
>> alternative which I was trying to avoid but at least I know is of high 
>> performance and it fits right my needs.
>> When a connection is upgraded to websocket it is passed to an actor, also 
>> every message sent is forwarded to an Actor, Java 8 code snippet below:
>>
>>       vertx.createHttpServer().requestHandler(request -> {
>>         if ("/signal".equals(request.path())) {
>>           final ServerWebSocket socket = request.upgrade();
>>           final ActorRef actorRef = context().system().actorOf(
>>             // Using an internal non-blocking bounded mailbox with 
>> capacity reserved (similar to LMAX)
>>             Props.create(new SignalWebsocketCreator(SourceSupervisor.this
>> , socket)).withMailbox("bounded-mailbox-1024")
>>           );
>>           socket.setWriteQueueMaxSize(1024).
>>             handler(event -> actorRef.tell(event, noSender())).
>>             closeHandler(event -> actorRef.tell(PoisonPill.getInstance(), 
>> noSender()));
>>           log.info("Websocket connection from '{}' to {} established.", 
>> socket.remoteAddress(), socket.localAddress());
>>         } else {
>>           request.response().setStatusCode(400).end();
>>         }
>>       }).listen(config.getInt("http.port"), config.getString("http.host"
>> ));
>>
>>
>> Hope it helps some Java fellows that are stuck on this matter, not ideal 
>> if you want to strictly stick to Akka like I wanted though it is still a 
>> quick, simple and efficient solution,
>>
>> Guido.
>>
>

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to