websockets with akka-http *java*

480 views
Skip to first unread message

ash.ta

unread,
Feb 8, 2016, 9:00:41 AM2/8/16
to Akka User List
hi hackers,

i am looking for a *java* api based  solution for websocket push functionality. clients will create websocket connections and server will push them messages periodically.  
as far as i understand it, i was planning to create an actor for each socket created, so that messages sent to this actor's address will be pushed through the websocket to the client.
unfortunately, the only doc i found is this: WebsocketRoutingExample here , but it provides a simple request-response example and i'm still missing the way an actor can be bound to Flow
or something like this.

i would really appreciate if you could point me to either an existing java example or to the set of apis that can help.

thanks!

Johan Andrén

unread,
Feb 8, 2016, 9:26:29 AM2/8/16
to Akka User List
Hi,

We have recently merged shiny new docs for the websocket client API,
they will be in the next release. If you are in a hurry you can find them
in master here:


--
Johan Andrén
Typesafe -  Reactive apps on the JVM
Twitter: @apnylle

ash.ta

unread,
Feb 8, 2016, 10:40:02 AM2/8/16
to Akka User List
thanks for a prompt response.
client is great, but i need server side implementation. and as i see, new server side docs still show the same basic request-response example. 

Johan Andrén

unread,
Feb 8, 2016, 10:54:48 AM2/8/16
to akka...@googlegroups.com
Sorry, I skimmed your question a little too quickly.

If you want to have a one to one with clients and actors, there is no
built in abstraction that makes it super easy, but it is possible using
Source.actorRef and Sink.actorRef. Here is an example which
is Scala but should point you in the right direction:


Sink.actorRef will take an ActorRef and simply direct messages to it so getting incoming messages to an actor 
is easy (you can also use actorRefWithAck and be able to provide backpressure). Source.actorRef will however
materialize into an actorRef that when you send messages to it, it will emit them. The trick is to get the materialized
actorRef into the flow, and send it to your connection actor as a message. (Another alternative would be to send it as
a message from mapMaterializedValue if you have the actorref for your connection actor in scope).

I hope this helps!
--
Johan Andrén
Typesafe -  Reactive apps on the JVM
Twitter: @apnylle

8 feb. 2016 kl. 16:40 skrev ash.ta <ata...@gmail.com>:

thanks for a prompt response.
client is great, but i need server side implementation. and as i see, new server side docs still show the same basic request-response example. 

--
>>>>>>>>>> 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 a topic in the Google Groups "Akka User List" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/akka-user/qWw79unk_tw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

ash.ta

unread,
Feb 9, 2016, 7:51:05 AM2/9/16
to Akka User List
hey johan,

thanks for explanation.
my main problem is that i don't know scala and the example is pretty much gibberish for me.
can you give me a hint about the flow point where sink and source can be called with actorref creation?
i mean, your java example shows 
handleWebsocketMessages()

which receives a mthode returning 

Flow<Message, Message, ?>

where and how sink and source can be bound to this flow?

thanks again.


 

Johan Andrén

unread,
Feb 9, 2016, 8:11:37 AM2/9/16
to akka...@googlegroups.com
The Java graph DSL isn’t that different, the squiggly arrows (~>) corresponds to builder.via and you might
need to manually perform some more builder.add calls to add stages than the Scala code does, but you
should be able to create something pretty much like it with Java.

The docs on the Java GraphDSL can be found here:

In the sample I gave you the Flow<Message, Message, ?> is created by first returning a FlowShape from the
block that creates the graph (in Scala the last value is returned, no return statement needed), and then passing the
resulting Graph<FlowShape<Message, Message,…>> to Flow.fromGraph.

If it wasn’t for the fact that we needed to pass the outgoing message actorref to the connection-actor we could
have just used Flow.fromSinkAndSource(sink, source) to construct it.

Johan Andrén
Typesafe -  Reactive apps on the JVM
Twitter: @apnylle

Reply all
Reply to author
Forward
0 new messages