[akka-user] Re: Akka HTTP Websockets Java with Actor

2018-03-04 Thread Brett Tofel
In case anyone is looking for an updated version of this code that will 
work with more current akka-http and Java:

https://gist.github.com/bentito/560eb95c64fa131efb34ad62c7bf60f8

-Brett Tofel

On Tuesday, March 8, 2016 at 9:59:26 AM UTC-5, Johan Andrén wrote:
>
> Here is an adaptation of the Scala sample, but in Java:
>
> import akka.NotUsed;
> import akka.actor.*;
> import akka.http.javadsl.model.ws.Message;
> import akka.http.javadsl.model.ws.TextMessage;
> import akka.http.javadsl.server.HttpApp;
> import akka.http.javadsl.server.Route;
> import akka.japi.pf.ReceiveBuilder;
> import akka.stream.OverflowStrategy;
> import akka.stream.javadsl.Flow;
> import akka.stream.javadsl.Sink;
> import akka.stream.javadsl.Source;
>
> import java.util.Optional;
>
> public class WebSocketServer {
>   private static final class Router extends HttpApp {
>
> private final ActorSystem system;
>
> public Router(ActorSystem system) {
>   this.system = system;
> }
>
> public Route createRoute() {
>   return route(
> path("test").route(
>   get(handleWebSocketMessages(createWebSocketFlow()))
> )
>   );
> }
>
> private Flow createWebSocketFlow() {
>   ActorRef actor = system.actorOf(Props.create(AnActor.class));
>
>   Source source = Source.actorRef(5, 
> OverflowStrategy.fail())
> .map((outgoing) -> (Message) TextMessage.create(outgoing.message))
> .mapMaterializedValue(destinationRef -> {
>   actor.tell(new OutgoingDestination(destinationRef), 
> ActorRef.noSender());
>   return NotUsed.getInstance();
> });
>
>   Sink sink = Flow.create()
> .map((msg) -> new Incoming(msg.asTextMessage().getStrictText()))
> .to(Sink.actorRef(actor, PoisonPill.getInstance()));
>
>
>   return Flow.fromSinkAndSource(sink, source);
> }
>
>   }
>
>
>
>
> public static void main(String[] args) {
> ActorSystem actorSystem = ActorSystem.create();
>
> Router router = new Router(actorSystem);
> router.bindRoute("127.0.0.1", 8082, actorSystem);
> }
>
>   static class Incoming {
> public final String message;
> public Incoming(String message) {
>   this.message = message;
> }
>   }
>
>   static class Outgoing {
> public final String message;
> public Outgoing(String message) {
>   this.message = message;
> }
>   }
>
>   static class OutgoingDestination {
> public final ActorRef destination;
> OutgoingDestination(ActorRef destination) {
>   this.destination = destination;
> }
>   }
>
>   static class AnActor extends AbstractActor {
>
> private Optional outgoing = Optional.empty();
>
> public AnActor() {
>   receive(ReceiveBuilder.match(
> OutgoingDestination.class, (msg) -> outgoing = 
> Optional.of(msg.destination)
>   ).match(
> Incoming.class, (in) -> outgoing.ifPresent((out) -> out.tell(new 
> Outgoing("got it"), self()))
>   ).build());
> }
>   }
> }
>
>
> Hope this helps.
>
> --
> Johan Andrén
> Akka Team, Lightbend Inc.
>

-- 
>>  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 akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


Re: [akka-user] Re: Akka HTTP Websockets Java with Actor

2016-05-12 Thread sergey . zakharov
We ended up creating flow per connection which is not ideal, but this flow 
holds reference to the actor and session id. The actor receives OpenSession 
event with unique session ID and reference to the actor to send replies, 
creates child to handle the session, and then dispatches all incoming 
messages to it.

I hope the flow is not reused internally and materialized only once, 
otherwise this code is broken.


@Override
public Route createRoute() {
return route(
get(path("data").route(handleWith(this::websocketHandler;
}

private RouteResult websocketHandler(RequestContext ctx) throws Exception {
int newSessionId = sessionId.incrementAndGet();
Source source =
Source.actorRef(BUFFER_SIZE, OverflowStrategy.fail()).
mapMaterializedValue(receiver -> {
webActor.tell(
 new WebSocketConnectionActor.OpenSession(newSessionId, 
receiver),
 ActorRef.noSender());
return receiver;
});
Sink actorSink =
Sink.actorRef(webActor, new WebSocketConnectionActor.CloseSession(
newSessionId));
Sink sink =
Flow.fromFunction
(
message -> new WebSocketConnectionActor.IncomingMessage(
newSessionId, message)).to(actorSink);
return ctx.complete(WebSocket.handleWebSocketRequestWith(ctx.request(), 
Flow.fromSinkAndSource(sink, source)));
}




On Tuesday, April 12, 2016 at 9:26:25 AM UTC+1, Alan Klikic wrote:
Hi all,
any feedback on this?

Enter code here...

Thank you in advance.
Br,
Alan
Dana petak, 1. travnja 2016. u 15:53:17 UTC+2, korisnik Alan Klikic napisao 
je:
Hi Endre,
thank you for the info.
All,
I tested this example and noticed that AnActor is instanced only ones when 
Router.cpublic Route createRoute() {
 return route(
 get(
 path("data").route(
 handleWith(this::websocketHandler)
 )
 )
 );
}
private RouteResult websocketHandler(RequestContext ctx) throws Exception {
 int newSessionId = sessionId.incrementAndGet();
 Source source =
 Source.actorRef(BUFFER_SIZE, OverflowStrategy.fail()).
 mapMaterializedValue(receiver -> {
 webActor.tell(
 new WebSocketConnectionActor.OpenSession(newSessionId, receiver),
 ActorRef.noSender());
 return receiver;
 });
 Sink actorSink =
 Sink.actorRef(webActor, new 
WebSocketConnectionActor.CloseSession(newSessionId));
 Sink sink =
 Flow.fromFunction(
 message -> new WebSocketConnectionActor.IncomingMessage(newSessionId, 
message)).to(actorSink);
 return ctx.complete(WebSocket.handleWebSocketRequestWith(ctx.request(), 
Flow.fromSinkAndSource(sink, source)));
}



On Tuesday, April 12, 2016 at 9:26:25 AM UTC+1, Alan Klikic wrote:
Hi all,

any feedback on this?
Thank you in advance.

Br,
Alan

Dana petak, 1. travnja 2016. u 15:53:17 UTC+2, korisnik Alan Klikic napisao 
je:
Hi Endre,

thank you for the info.

All,

I tested this example and noticed that AnActor is instanced only ones when R

Enter code here...
outer.createRoute is called.
Does this mean that one instance of AnActor is used for all websocket 
connections (if you have multiple parallel connections)?
I thought that AnActor represents each websocket connection (so multiple 
AnActor instances are created when you have multiple websocket connections).

Thank you in advance.

P.S. I'm new with akka and more and more enthusiastic about it while 
learning it. Also community is great. Thanks to all :)

Br,
Alan

Dana četvrtak, 31. ožujka 2016. u 16:12:12 UTC+2, korisnik drewhk napisao 
je:




On Thu, Mar 31, 2016 at 4:08 PM, Alan Klikic  wrote:

Hi all,

 Sink sink = Flow.create()
.map((msg) -> new Incoming(msg.asTextMessage().getStrictText()))
.to(Sink.actorRef(actor, PoisonPill.getInstance()));

Can you please explain what does bolded part of the code do? 
My first thought was that PoisonPill is sent when websocket disconnects. 


Yes, that is it. You can specify the stop message that is sent to the actor 
on upstream completion, which is in this case the built-in PoisonPill 
message -- but you can of course change this to whatever you want.


-Endre


 
Please correct me if I'm wrong.

Thank you in advance.

BR,
Alan

Dana utorak, 8. ožujka 2016. u 15:59:26 UTC+1, korisnik Johan Andrén 
napisao je:

Here is an adaptation of the Scala sample, but in Java:


import akka.NotUsed;
import akka.actor.*;
import akka.http.javadsl.model.ws.Message;
import akka.http.javadsl.model.ws.TextMessage;
import akka.http.javadsl.server.HttpApp;
import akka.http.javadsl.server.Route;
import akka.japi.pf.ReceiveBuilder;
import akka.stream.OverflowStrategy;
import akka.stream.javadsl.Flow;
import akka.stream.javadsl.Sink;
import akka.stream.javadsl.Source;

import 

Re: [akka-user] Re: Akka HTTP Websockets Java with Actor

2016-04-12 Thread Alan Klikic
Hi all,

any feedback on this?
Thank you in advance.

Br,
Alan

Dana petak, 1. travnja 2016. u 15:53:17 UTC+2, korisnik Alan Klikic napisao 
je:
>
> Hi Endre,
>
> thank you for the info.
>
> All,
>
> I tested this example and noticed that AnActor is instanced only ones when 
> Router.createRoute is called.
> Does this mean that one instance of AnActor is used for all websocket 
> connections (if you have multiple parallel connections)?
> I thought that AnActor represents each websocket connection (so multiple 
> AnActor instances are created when you have multiple websocket connections).
>
> Thank you in advance.
>
> P.S. I'm new with akka and more and more enthusiastic about it while 
> learning it. Also community is great. Thanks to all :)
>
> Br,
> Alan
>
> Dana četvrtak, 31. ožujka 2016. u 16:12:12 UTC+2, korisnik drewhk napisao 
> je:
>>
>>
>>
>> On Thu, Mar 31, 2016 at 4:08 PM, Alan Klikic  wrote:
>>
>>> Hi all,
>>>
>>>  Sink sink = Flow.create()
>>> .map((msg) -> new Incoming(msg.asTextMessage().getStrictText()))
>>>* .to(Sink.actorRef(actor, PoisonPill.getInstance()));*
>>>
>>> Can you please explain what does bolded part of the code do? 
>>> My first thought was that PoisonPill is sent when websocket disconnects. 
>>>
>>
>> Yes, that is it. You can specify the stop message that is sent to the 
>> actor on upstream completion, which is in this case the built-in PoisonPill 
>> message -- but you can of course change this to whatever you want.
>>
>> -Endre
>>
>>  
>>
>>> Please correct me if I'm wrong.
>>>
>>> Thank you in advance.
>>>
>>> BR,
>>> Alan
>>>
>>> Dana utorak, 8. ožujka 2016. u 15:59:26 UTC+1, korisnik Johan Andrén 
>>> napisao je:
>>>
 Here is an adaptation of the Scala sample, but in Java:

 import akka.NotUsed;
 import akka.actor.*;
 import akka.http.javadsl.model.ws.Message;
 import akka.http.javadsl.model.ws.TextMessage;
 import akka.http.javadsl.server.HttpApp;
 import akka.http.javadsl.server.Route;
 import akka.japi.pf.ReceiveBuilder;
 import akka.stream.OverflowStrategy;
 import akka.stream.javadsl.Flow;
 import akka.stream.javadsl.Sink;
 import akka.stream.javadsl.Source;

 import java.util.Optional;

 public class WebSocketServer {
   private static final class Router extends HttpApp {

 private final ActorSystem system;

 public Router(ActorSystem system) {
   this.system = system;
 }

 public Route createRoute() {
   return route(
 path("test").route(
   get(handleWebSocketMessages(createWebSocketFlow()))
 )
   );
 }

 private Flow createWebSocketFlow() {
   ActorRef actor = system.actorOf(Props.create(AnActor.class));

   Source source = Source.actorRef(5, 
 OverflowStrategy.fail())
 .map((outgoing) -> (Message) TextMessage.create(outgoing.message))
 .mapMaterializedValue(destinationRef -> {
   actor.tell(new OutgoingDestination(destinationRef), 
 ActorRef.noSender());
   return NotUsed.getInstance();
 });

   Sink sink = Flow.create()
 .map((msg) -> new Incoming(msg.asTextMessage().getStrictText()))
 .to(Sink.actorRef(actor, PoisonPill.getInstance()));


   return Flow.fromSinkAndSource(sink, source);
 }

   }




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

 Router router = new Router(actorSystem);
 router.bindRoute("127.0.0.1", 8082, actorSystem);
 }

   static class Incoming {
 public final String message;
 public Incoming(String message) {
   this.message = message;
 }
   }

   static class Outgoing {
 public final String message;
 public Outgoing(String message) {
   this.message = message;
 }
   }

   static class OutgoingDestination {
 public final ActorRef destination;
 OutgoingDestination(ActorRef destination) {
   this.destination = destination;
 }
   }

   static class AnActor extends AbstractActor {

 private Optional outgoing = Optional.empty();

 public AnActor() {
   receive(ReceiveBuilder.match(
 OutgoingDestination.class, (msg) -> outgoing = 
 Optional.of(msg.destination)
   ).match(
 Incoming.class, (in) -> outgoing.ifPresent((out) -> out.tell(new 
 Outgoing("got it"), self()))
   ).build());
 }
   }
 }


 Hope this helps.

 --
 Johan Andrén
 Akka Team, Lightbend Inc.

>>> -- 
>>> >> Read the docs: 

Re: [akka-user] Re: Akka HTTP Websockets Java with Actor

2016-04-01 Thread Alan Klikic
Hi Endre,

thank you for the info.

All,

I tested this example and noticed that AnActor is instanced only ones when 
Router.createRoute is called.
Does this mean that one instance of AnActor is used for all websocket 
connections (if you have multiple parallel connections)?
I thought that AnActor represents each websocket connection (so multiple 
AnActor instances are created when you have multiple websocket connections).

Thank you in advance.

P.S. I'm new with akka and more and more enthusiastic about it while 
learning it. Also community is great. Thanks to all :)

Br,
Alan

Dana četvrtak, 31. ožujka 2016. u 16:12:12 UTC+2, korisnik drewhk napisao 
je:
>
>
>
> On Thu, Mar 31, 2016 at 4:08 PM, Alan Klikic  > wrote:
>
>> Hi all,
>>
>>  Sink sink = Flow.create()
>> .map((msg) -> new Incoming(msg.asTextMessage().getStrictText()))
>>* .to(Sink.actorRef(actor, PoisonPill.getInstance()));*
>>
>> Can you please explain what does bolded part of the code do? 
>> My first thought was that PoisonPill is sent when websocket disconnects. 
>>
>
> Yes, that is it. You can specify the stop message that is sent to the 
> actor on upstream completion, which is in this case the built-in PoisonPill 
> message -- but you can of course change this to whatever you want.
>
> -Endre
>
>  
>
>> Please correct me if I'm wrong.
>>
>> Thank you in advance.
>>
>> BR,
>> Alan
>>
>> Dana utorak, 8. ožujka 2016. u 15:59:26 UTC+1, korisnik Johan Andrén 
>> napisao je:
>>
>>> Here is an adaptation of the Scala sample, but in Java:
>>>
>>> import akka.NotUsed;
>>> import akka.actor.*;
>>> import akka.http.javadsl.model.ws.Message;
>>> import akka.http.javadsl.model.ws.TextMessage;
>>> import akka.http.javadsl.server.HttpApp;
>>> import akka.http.javadsl.server.Route;
>>> import akka.japi.pf.ReceiveBuilder;
>>> import akka.stream.OverflowStrategy;
>>> import akka.stream.javadsl.Flow;
>>> import akka.stream.javadsl.Sink;
>>> import akka.stream.javadsl.Source;
>>>
>>> import java.util.Optional;
>>>
>>> public class WebSocketServer {
>>>   private static final class Router extends HttpApp {
>>>
>>> private final ActorSystem system;
>>>
>>> public Router(ActorSystem system) {
>>>   this.system = system;
>>> }
>>>
>>> public Route createRoute() {
>>>   return route(
>>> path("test").route(
>>>   get(handleWebSocketMessages(createWebSocketFlow()))
>>> )
>>>   );
>>> }
>>>
>>> private Flow createWebSocketFlow() {
>>>   ActorRef actor = system.actorOf(Props.create(AnActor.class));
>>>
>>>   Source source = Source.actorRef(5, 
>>> OverflowStrategy.fail())
>>> .map((outgoing) -> (Message) TextMessage.create(outgoing.message))
>>> .mapMaterializedValue(destinationRef -> {
>>>   actor.tell(new OutgoingDestination(destinationRef), 
>>> ActorRef.noSender());
>>>   return NotUsed.getInstance();
>>> });
>>>
>>>   Sink sink = Flow.create()
>>> .map((msg) -> new Incoming(msg.asTextMessage().getStrictText()))
>>> .to(Sink.actorRef(actor, PoisonPill.getInstance()));
>>>
>>>
>>>   return Flow.fromSinkAndSource(sink, source);
>>> }
>>>
>>>   }
>>>
>>>
>>>
>>>
>>> public static void main(String[] args) {
>>> ActorSystem actorSystem = ActorSystem.create();
>>>
>>> Router router = new Router(actorSystem);
>>> router.bindRoute("127.0.0.1", 8082, actorSystem);
>>> }
>>>
>>>   static class Incoming {
>>> public final String message;
>>> public Incoming(String message) {
>>>   this.message = message;
>>> }
>>>   }
>>>
>>>   static class Outgoing {
>>> public final String message;
>>> public Outgoing(String message) {
>>>   this.message = message;
>>> }
>>>   }
>>>
>>>   static class OutgoingDestination {
>>> public final ActorRef destination;
>>> OutgoingDestination(ActorRef destination) {
>>>   this.destination = destination;
>>> }
>>>   }
>>>
>>>   static class AnActor extends AbstractActor {
>>>
>>> private Optional outgoing = Optional.empty();
>>>
>>> public AnActor() {
>>>   receive(ReceiveBuilder.match(
>>> OutgoingDestination.class, (msg) -> outgoing = 
>>> Optional.of(msg.destination)
>>>   ).match(
>>> Incoming.class, (in) -> outgoing.ifPresent((out) -> out.tell(new 
>>> Outgoing("got it"), self()))
>>>   ).build());
>>> }
>>>   }
>>> }
>>>
>>>
>>> Hope this helps.
>>>
>>> --
>>> Johan Andrén
>>> Akka Team, Lightbend Inc.
>>>
>> -- 
>> >> 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 

Re: [akka-user] Re: Akka HTTP Websockets Java with Actor

2016-03-31 Thread Endre Varga
On Thu, Mar 31, 2016 at 4:08 PM, Alan Klikic  wrote:

> Hi all,
>
>  Sink sink = Flow.create()
> .map((msg) -> new Incoming(msg.asTextMessage().getStrictText()))
>* .to(Sink.actorRef(actor, PoisonPill.getInstance()));*
>
> Can you please explain what does bolded part of the code do?
> My first thought was that PoisonPill is sent when websocket disconnects.
>

Yes, that is it. You can specify the stop message that is sent to the actor
on upstream completion, which is in this case the built-in PoisonPill
message -- but you can of course change this to whatever you want.

-Endre



> Please correct me if I'm wrong.
>
> Thank you in advance.
>
> BR,
> Alan
>
> Dana utorak, 8. ožujka 2016. u 15:59:26 UTC+1, korisnik Johan Andrén
> napisao je:
>
>> Here is an adaptation of the Scala sample, but in Java:
>>
>> import akka.NotUsed;
>> import akka.actor.*;
>> import akka.http.javadsl.model.ws.Message;
>> import akka.http.javadsl.model.ws.TextMessage;
>> import akka.http.javadsl.server.HttpApp;
>> import akka.http.javadsl.server.Route;
>> import akka.japi.pf.ReceiveBuilder;
>> import akka.stream.OverflowStrategy;
>> import akka.stream.javadsl.Flow;
>> import akka.stream.javadsl.Sink;
>> import akka.stream.javadsl.Source;
>>
>> import java.util.Optional;
>>
>> public class WebSocketServer {
>>   private static final class Router extends HttpApp {
>>
>> private final ActorSystem system;
>>
>> public Router(ActorSystem system) {
>>   this.system = system;
>> }
>>
>> public Route createRoute() {
>>   return route(
>> path("test").route(
>>   get(handleWebSocketMessages(createWebSocketFlow()))
>> )
>>   );
>> }
>>
>> private Flow createWebSocketFlow() {
>>   ActorRef actor = system.actorOf(Props.create(AnActor.class));
>>
>>   Source source = Source.actorRef(5, 
>> OverflowStrategy.fail())
>> .map((outgoing) -> (Message) TextMessage.create(outgoing.message))
>> .mapMaterializedValue(destinationRef -> {
>>   actor.tell(new OutgoingDestination(destinationRef), 
>> ActorRef.noSender());
>>   return NotUsed.getInstance();
>> });
>>
>>   Sink sink = Flow.create()
>> .map((msg) -> new Incoming(msg.asTextMessage().getStrictText()))
>> .to(Sink.actorRef(actor, PoisonPill.getInstance()));
>>
>>
>>   return Flow.fromSinkAndSource(sink, source);
>> }
>>
>>   }
>>
>>
>>
>>
>> public static void main(String[] args) {
>> ActorSystem actorSystem = ActorSystem.create();
>>
>> Router router = new Router(actorSystem);
>> router.bindRoute("127.0.0.1", 8082, actorSystem);
>> }
>>
>>   static class Incoming {
>> public final String message;
>> public Incoming(String message) {
>>   this.message = message;
>> }
>>   }
>>
>>   static class Outgoing {
>> public final String message;
>> public Outgoing(String message) {
>>   this.message = message;
>> }
>>   }
>>
>>   static class OutgoingDestination {
>> public final ActorRef destination;
>> OutgoingDestination(ActorRef destination) {
>>   this.destination = destination;
>> }
>>   }
>>
>>   static class AnActor extends AbstractActor {
>>
>> private Optional outgoing = Optional.empty();
>>
>> public AnActor() {
>>   receive(ReceiveBuilder.match(
>> OutgoingDestination.class, (msg) -> outgoing = 
>> Optional.of(msg.destination)
>>   ).match(
>> Incoming.class, (in) -> outgoing.ifPresent((out) -> out.tell(new 
>> Outgoing("got it"), self()))
>>   ).build());
>> }
>>   }
>> }
>>
>>
>> Hope this helps.
>>
>> --
>> Johan Andrén
>> Akka Team, Lightbend Inc.
>>
> --
> >> 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 akka-user+unsubscr...@googlegroups.com.
> To post to this group, send email to akka-user@googlegroups.com.
> Visit this group at https://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
>>  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 akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at 

[akka-user] Re: Akka HTTP Websockets Java with Actor

2016-03-31 Thread Alan Klikic
Hi all,

 Sink sink = Flow.create()
.map((msg) -> new Incoming(msg.asTextMessage().getStrictText()))
   * .to(Sink.actorRef(actor, PoisonPill.getInstance()));*

Can you please explain what does bolded part of the code do? 
My first thought was that PoisonPill is sent when websocket disconnects. 
Please correct me if I'm wrong.

Thank you in advance.

BR,
Alan

Dana utorak, 8. ožujka 2016. u 15:59:26 UTC+1, korisnik Johan Andrén 
napisao je:
>
> Here is an adaptation of the Scala sample, but in Java:
>
> import akka.NotUsed;
> import akka.actor.*;
> import akka.http.javadsl.model.ws.Message;
> import akka.http.javadsl.model.ws.TextMessage;
> import akka.http.javadsl.server.HttpApp;
> import akka.http.javadsl.server.Route;
> import akka.japi.pf.ReceiveBuilder;
> import akka.stream.OverflowStrategy;
> import akka.stream.javadsl.Flow;
> import akka.stream.javadsl.Sink;
> import akka.stream.javadsl.Source;
>
> import java.util.Optional;
>
> public class WebSocketServer {
>   private static final class Router extends HttpApp {
>
> private final ActorSystem system;
>
> public Router(ActorSystem system) {
>   this.system = system;
> }
>
> public Route createRoute() {
>   return route(
> path("test").route(
>   get(handleWebSocketMessages(createWebSocketFlow()))
> )
>   );
> }
>
> private Flow createWebSocketFlow() {
>   ActorRef actor = system.actorOf(Props.create(AnActor.class));
>
>   Source source = Source.actorRef(5, 
> OverflowStrategy.fail())
> .map((outgoing) -> (Message) TextMessage.create(outgoing.message))
> .mapMaterializedValue(destinationRef -> {
>   actor.tell(new OutgoingDestination(destinationRef), 
> ActorRef.noSender());
>   return NotUsed.getInstance();
> });
>
>   Sink sink = Flow.create()
> .map((msg) -> new Incoming(msg.asTextMessage().getStrictText()))
> .to(Sink.actorRef(actor, PoisonPill.getInstance()));
>
>
>   return Flow.fromSinkAndSource(sink, source);
> }
>
>   }
>
>
>
>
> public static void main(String[] args) {
> ActorSystem actorSystem = ActorSystem.create();
>
> Router router = new Router(actorSystem);
> router.bindRoute("127.0.0.1", 8082, actorSystem);
> }
>
>   static class Incoming {
> public final String message;
> public Incoming(String message) {
>   this.message = message;
> }
>   }
>
>   static class Outgoing {
> public final String message;
> public Outgoing(String message) {
>   this.message = message;
> }
>   }
>
>   static class OutgoingDestination {
> public final ActorRef destination;
> OutgoingDestination(ActorRef destination) {
>   this.destination = destination;
> }
>   }
>
>   static class AnActor extends AbstractActor {
>
> private Optional outgoing = Optional.empty();
>
> public AnActor() {
>   receive(ReceiveBuilder.match(
> OutgoingDestination.class, (msg) -> outgoing = 
> Optional.of(msg.destination)
>   ).match(
> Incoming.class, (in) -> outgoing.ifPresent((out) -> out.tell(new 
> Outgoing("got it"), self()))
>   ).build());
> }
>   }
> }
>
>
> Hope this helps.
>
> --
> Johan Andrén
> Akka Team, Lightbend Inc.
>

-- 
>>  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 akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


Re: [akka-user] Re: Akka HTTP Websockets Java with Actor

2016-03-20 Thread Konrad Malawski
New Java API is comign in very large steps, I think the new one you'll like
a lot and come back to plain Akka :-)


On Mon, Mar 14, 2016 at 5:13 PM, Guido Medina  wrote:

> Also there are other aspects you should be aware like back-pressure which
> is better handled at Akka, to be fair both teams are awesome (Vert.x and
> Akka)
> But in my case my decision was influenced by the fact that my company has
> 1 man team for the project => me, so I need things to be done faster than
> normal workflow.
> Also I'm streaming many small messages (prices) and they need to get to
> the client fast so speed is an important factor for me.
>
> HTH,
>
> Guido.
>
>
> On Monday, March 14, 2016 at 4:03:41 PM UTC, Guido Medina wrote:
>>
>> Vert.x is just another reactive framework, I found it easier to use than
>> Akka HTTP for Java that's all, and I think (at least today) it is faster.
>> I cannot really recommend you one or the other just state what I know,
>> the decision is still yours.
>> I know Akka team works very hard on improving things so always be open
>> and treat my recommendation as a biased opinion.
>>
>> HTH,
>>
>> Guido.
>>
>> On Monday, March 14, 2016 at 11:50:57 AM UTC, Alan Klikic wrote:
>>>
>>> Hi Guido,
>>>
>>> thank you for the shared info.
>>> We are planing to use Google's Protocol Buffer (
>>> https://developers.google.com/protocol-buffers/) instead of JSON.
>>> You recommend using Vort.x with Netty implementation instead of Akka
>>> HTTP for now?
>>>
>>> Br,
>>> Alan
>>>
>>> Dana srijeda, 9. ožujka 2016. u 11:11:43 UTC+1, korisnik Guido Medina
>>> napisao je:

 *Disclaimer:* The Akka HTTP performance on that page is outdated, now;
 if Akka HTTP is around 75% performance of Play 2, you can guess where it
 would be on that list.

 On Wednesday, March 9, 2016 at 9:58:04 AM UTC, Guido Medina wrote:
>
> 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=peak=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 

Re: [akka-user] Re: Akka HTTP Websockets Java with Actor

2016-03-19 Thread Guido Medina
Give me cookies first, oreos !!!
I'm sure I will, it is just a matter of time and spend sometime migrating, 
I just initially didn't have the time to be the first with headaches,
That said I think in a couple of weeks I should be moved over.

Thanks to Akka team and contributors for all the effort put here,

Guido.

On Friday, March 18, 2016 at 4:06:08 PM UTC, Konrad Malawski wrote:
>
> New Java API is comign in very large steps, I think the new one you'll 
> like a lot and come back to plain Akka :-)
>
>
> On Mon, Mar 14, 2016 at 5:13 PM, Guido Medina  > wrote:
>
>> Also there are other aspects you should be aware like back-pressure which 
>> is better handled at Akka, to be fair both teams are awesome (Vert.x and 
>> Akka)
>> But in my case my decision was influenced by the fact that my company has 
>> 1 man team for the project => me, so I need things to be done faster than 
>> normal workflow.
>> Also I'm streaming many small messages (prices) and they need to get to 
>> the client fast so speed is an important factor for me.
>>
>> HTH,
>>
>> Guido.
>>
>>
>> On Monday, March 14, 2016 at 4:03:41 PM UTC, Guido Medina wrote:
>>>
>>> Vert.x is just another reactive framework, I found it easier to use than 
>>> Akka HTTP for Java that's all, and I think (at least today) it is faster.
>>> I cannot really recommend you one or the other just state what I know, 
>>> the decision is still yours.
>>> I know Akka team works very hard on improving things so always be open 
>>> and treat my recommendation as a biased opinion.
>>>
>>> HTH,
>>>
>>> Guido.
>>>
>>> On Monday, March 14, 2016 at 11:50:57 AM UTC, Alan Klikic wrote:

 Hi Guido,

 thank you for the shared info.
 We are planing to use Google's Protocol Buffer (
 https://developers.google.com/protocol-buffers/) instead of JSON.
 You recommend using Vort.x with Netty implementation instead of Akka 
 HTTP for now?

 Br,
 Alan

 Dana srijeda, 9. ožujka 2016. u 11:11:43 UTC+1, korisnik Guido Medina 
 napisao je:
>
> *Disclaimer:* The Akka HTTP performance on that page is outdated, 
> now; if Akka HTTP is around 75% performance of Play 2, you can guess 
> where 
> it would be on that list.
>
> On Wednesday, March 9, 2016 at 9:58:04 AM UTC, Guido Medina wrote:
>>
>> 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=peak=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 

[akka-user] Re: Akka HTTP Websockets Java with Actor

2016-03-14 Thread Guido Medina
Vert.x is just another reactive framework, I found it easier to use than 
Akka HTTP for Java that's all, and I think (at least today) it is faster.
I cannot really recommend you one or the other just state what I know, the 
decision is still yours.
I know Akka team works very hard on improving things so always be open and 
treat my recommendation as a biased opinion.

HTH,

Guido.

On Monday, March 14, 2016 at 11:50:57 AM UTC, Alan Klikic wrote:
>
> Hi Guido,
>
> thank you for the shared info.
> We are planing to use Google's Protocol Buffer (
> https://developers.google.com/protocol-buffers/) instead of JSON.
> You recommend using Vort.x with Netty implementation instead of Akka HTTP 
> for now?
>
> Br,
> Alan
>
> Dana srijeda, 9. ožujka 2016. u 11:11:43 UTC+1, korisnik Guido Medina 
> napisao je:
>>
>> *Disclaimer:* The Akka HTTP performance on that page is outdated, now; 
>> if Akka HTTP is around 75% performance of Play 2, you can guess where it 
>> would be on that list.
>>
>> On Wednesday, March 9, 2016 at 9:58:04 AM UTC, Guido Medina wrote:
>>>
>>> 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=peak=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()));
> 

[akka-user] Re: Akka HTTP Websockets Java with Actor

2016-03-14 Thread Alan Klikic
Hi Guido,

thank you for the shared info.
We are planing to use Google's Protocol Buffer 
(https://developers.google.com/protocol-buffers/) instead of JSON.
You recommend using Vort.x with Netty implementation instead of Akka HTTP 
for now?

Br,
Alan

Dana srijeda, 9. ožujka 2016. u 11:11:43 UTC+1, korisnik Guido Medina 
napisao je:
>
> *Disclaimer:* The Akka HTTP performance on that page is outdated, now; if 
> Akka HTTP is around 75% performance of Play 2, you can guess where it would 
> be on that list.
>
> On Wednesday, March 9, 2016 at 9:58:04 AM UTC, Guido Medina wrote:
>>
>> 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=peak=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: 

[akka-user] Re: Akka HTTP Websockets Java with Actor

2016-03-09 Thread Guido Medina
*Disclaimer:* The Akka HTTP performance on that page is outdated, now; if 
Akka HTTP is around 75% performance of Play 2, you can guess where it would 
be on that list.

On Wednesday, March 9, 2016 at 9:58:04 AM UTC, Guido Medina wrote:
>
> 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=peak=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 akka-user+unsubscr...@googlegroups.com.
To post to this group, send email 

[akka-user] Re: Akka HTTP Websockets Java with Actor

2016-03-08 Thread Johan Andrén
Here is an adaptation of the Scala sample, but in Java:

import akka.NotUsed;
import akka.actor.*;
import akka.http.javadsl.model.ws.Message;
import akka.http.javadsl.model.ws.TextMessage;
import akka.http.javadsl.server.HttpApp;
import akka.http.javadsl.server.Route;
import akka.japi.pf.ReceiveBuilder;
import akka.stream.OverflowStrategy;
import akka.stream.javadsl.Flow;
import akka.stream.javadsl.Sink;
import akka.stream.javadsl.Source;

import java.util.Optional;

public class WebSocketServer {
  private static final class Router extends HttpApp {

private final ActorSystem system;

public Router(ActorSystem system) {
  this.system = system;
}

public Route createRoute() {
  return route(
path("test").route(
  get(handleWebSocketMessages(createWebSocketFlow()))
)
  );
}

private Flow createWebSocketFlow() {
  ActorRef actor = system.actorOf(Props.create(AnActor.class));

  Source source = Source.actorRef(5, 
OverflowStrategy.fail())
.map((outgoing) -> (Message) TextMessage.create(outgoing.message))
.mapMaterializedValue(destinationRef -> {
  actor.tell(new OutgoingDestination(destinationRef), 
ActorRef.noSender());
  return NotUsed.getInstance();
});

  Sink sink = Flow.create()
.map((msg) -> new Incoming(msg.asTextMessage().getStrictText()))
.to(Sink.actorRef(actor, PoisonPill.getInstance()));


  return Flow.fromSinkAndSource(sink, source);
}

  }




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

Router router = new Router(actorSystem);
router.bindRoute("127.0.0.1", 8082, actorSystem);
}

  static class Incoming {
public final String message;
public Incoming(String message) {
  this.message = message;
}
  }

  static class Outgoing {
public final String message;
public Outgoing(String message) {
  this.message = message;
}
  }

  static class OutgoingDestination {
public final ActorRef destination;
OutgoingDestination(ActorRef destination) {
  this.destination = destination;
}
  }

  static class AnActor extends AbstractActor {

private Optional outgoing = Optional.empty();

public AnActor() {
  receive(ReceiveBuilder.match(
OutgoingDestination.class, (msg) -> outgoing = 
Optional.of(msg.destination)
  ).match(
Incoming.class, (in) -> outgoing.ifPresent((out) -> out.tell(new 
Outgoing("got it"), self()))
  ).build());
}
  }
}


Hope this helps.

--
Johan Andrén
Akka Team, Lightbend Inc.

-- 
>>  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 akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


[akka-user] Re: Akka HTTP Websockets Java with Actor

2016-03-08 Thread code . patrol
It also may be helpful to check this blog post 
http://blog.scalac.io/2015/07/30/websockets-server-with-akka-http.html and 
its source code https://github.com/ScalaConsultants/wsug-akka-websockets

On Tuesday, February 23, 2016 at 3:33:18 PM UTC+3, Guido Medina wrote:
>
> Hi,
>
> Has anyone been able to write an Akka HTTP websockets server that 
> delegates/relays the established connection to an existing or newly created 
> actor?
> where messages sent from that connection comes to an actor and messages to 
> another actor? (I'm not sure how is this done behind the scenes if 1 or 2 
> actors for Source/Sink)
>
> I was given this example 
> https://github.com/johanandren/scala-stockholm-cluster-message-broker/blob/solution/src/main/scala/WebServer.scala
> But I can't make reproduce that code in Java so I'm stuck.
>
> I have been wanting/asking this since version 1.0 of Akka HTTP, am I blind 
> or something that can't figure this out?
> It makes me wonder if Java is that helpless in the Akka world.
>
> Any Java example even pieces that can lead me to it will be of great help,
>
> 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 akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


[akka-user] Re: Akka HTTP Websockets Java with Actor

2016-03-08 Thread Alan Klikic
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 akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


[akka-user] Re: Akka HTTP Websockets Java with Actor

2016-02-24 Thread Guido Medina
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 akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.