Play Framework WebSockets, how to generate event outside of webapp

733 views
Skip to first unread message

matthew.hi...@gmail.com

unread,
Oct 3, 2013, 12:10:30 PM10/3/13
to play-fr...@googlegroups.com
Hi all,

I am interested in using Play Framework to build a streaming API like Twitter has for a project I'm assigned to.

My question is fairly straight forward. Events for when I have data available to stream are not being generated by clients as they do in traditional bi-directional websocket applications such as a 'chat' program. In my case, someone might enter data into a database and I'd have a trigger that needs to send a broadcast to all connected clients. Or I might have a file added to a directory that has a JSON message in it that I need to broadcast to all connected clients.

Whatever the external factor is, I need a way to communicate with Play Framework in order to be able to broadcast messages to clients. Ideally, clients will be registered to a topic in something like Redis pub/sub and I can simply push events via Redis. Not sure if Play has any plugins currently for websockets to work with redis pubsub, but that would be a super convenient way to build what I'm working on.

Surely someone has used Play to communicate with connected clients with events generated outside of the webapp itself. What are some current solutions out there to look at and study in order to go forward with this?

Thank you.

Donovan Muller

unread,
Oct 3, 2013, 2:50:02 PM10/3/13
to play-fr...@googlegroups.com
Hi Mathew,

I am currently working on a project where notification events are received and broadcast to clients connected to a Play app via websockets.
We are using Redis for the pub/sub part and Akka actors with websockets for managing broadcasts to the connected clients.

So, in a nutshell:
  • When the Play app instance starts up it subscribes to the notifications "topic" on Redis
  • When a client initiates a websocket connection, we fire up a unique actor per websocket connection (we support multiple websocket connections per page etc.)
  • When a notification is published to Redis, it is received by the subscribed Play app and fired off to all current actors (websocket connections), who (actor) depending on whether they interested in that type of notification or not then either ignores the notification or writes the notification to the open websocket, which is then handled as the page needs.
  • If a websocket connection is closed then the actor representing that connection is shutdown.
This all happens in async goodness and has proven to be quite robust in production.
Hope that gives you some ideas...

Cheers


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

matthew.hi...@gmail.com

unread,
Oct 3, 2013, 3:58:48 PM10/3/13
to play-fr...@googlegroups.com
Thanks for the reply, Donovan.

I do wish there was some sample code out there. Going to be difficult as a new comer to Scala + Play to figure this out. 

I'll take your points and break them down step by step and see how far I can get. Would be great if anyone else reading this post has some advice.

Thanks much.

Saurabh Rawat

unread,
Oct 4, 2013, 1:29:12 AM10/4/13
to play-fr...@googlegroups.com
Hi,

I don't know how much it might suit your needs but you might want to take a look at https://github.com/eklavya/socket.io.play

Many of the changes are yet to be merged in upstream, so you'll have to compile and publish it locally. As a benefit you can use any client whether it supports websockets or not.

Mail me if you need any help setting up.

Regards,

Saurabh Rawat

John Simone

unread,
Oct 4, 2013, 2:35:26 AM10/4/13
to play-fr...@googlegroups.com
Matt,

I've been working on a version of the websocket chat app that does what you're talking about: https://github.com/jsimone/play-websockets-chat-sample

It's in Java so not exactly what you're looking for.

My inspiration comes from a Scala version with a slightly different implementation done by Ryan Tanner: https://github.com/ryantanner/websocketchat-redis and some websocket redis pub/sub work done by Pere Villega: http://perevillega.com/post/2013/02/24/27/using-redis-pubsub-with-play-210/

Those should help get you going.

- John

Pascal Voitot Dev

unread,
Oct 4, 2013, 2:45:26 AM10/4/13
to play-fr...@googlegroups.com

I recently released a helper to facilitate websocket (or others channels later) room management in play2: play actor-room
http://mandubian.com/2013/09/22/play-actor-room/

It's just a helper exposing only actors and managing all the connection events.

Regards
Pascal

matthew.hi...@gmail.com

unread,
Oct 4, 2013, 3:57:48 PM10/4/13
to play-fr...@googlegroups.com
Thanks so much everyone. There's a wealth of information and source code here to review to get me started. This is great stuff. Appreciate all the responses.

ryan

unread,
Oct 5, 2013, 3:51:37 PM10/5/13
to play-fr...@googlegroups.com
Take a look at the Reactive Stocks Template in Activator.  It walks through doing something very similar to this with Play and Akka Actors.  It simulates this type of scenario by firing stock updates on a periodic basis and those updates get broadcast to the client via web sockets.

matthew.hi...@gmail.com

unread,
Oct 6, 2013, 12:09:43 PM10/6/13
to play-fr...@googlegroups.com
Mind posting a URL Ryan? Not sure where that's located. 

Thanks so much.

Will Sargent

unread,
Oct 6, 2013, 11:42:23 PM10/6/13
to play-fr...@googlegroups.com

James Ward

unread,
Oct 7, 2013, 9:47:09 AM10/7/13
to play-fr...@googlegroups.com
You can learn more about the template at:
http://typesafe.com/activator/template/reactive-stocks

-James
> <mailto:play-framework%2Bunsu...@googlegroups.com>.
Reply all
Reply to author
Forward
0 new messages