Question

I'm working on a twitter app right now using twitter4j. Basically I am allowing users to create accounts, add their personal keywords, and start their own stream.

I'm trying to play as nice as possible with the twitter api. Avoid rate limits, don't be connecting the same account over and over etc. So what I think I need to do is have some object that contains a list of all the active TwitterStream object, but I don't know how to approach this. This is the controller to start the stream.

public static Result startStream(){ 
    ObjectNode result = Json.newObject();
    if (
        //openStreams is a Map<Long,TwitterStream> in the TwitterListener class
        TwitterListener.openStreams.containsKey(Long.parseLong(session().get("id")))
        ){
            result.put("status", "running");
            return ok(result);
    }
    Cache.set("twitterStream", TwitterListener.listener(
            Person.find.byId(
                    Long.parseLong(session().get("id"))
                    )
                )
            );
    result.put("status", "OK");
    return ok(result);          
}

As you can see I am putting them in Cache right now but I'd like to keep streams open for long periods of time, so cache won't suffice.

  • What is the most appropriate way to structure my application for this purpose?

  • Should I be using Akka?

  • How could I implement Play's Global object to do this?

Was it helpful?

Solution

As soon as you start to think about introducing global state in your application, you have to ask yourself, is there any possibility that I might want to scale to multiple nodes, or have multiple nodes for the purposes of redundancy? If there's even the slightest chance that the answer is yes, then you should use Akka, because Akka will allow you to easily adapt your code to work in a multi node environment with Akka clustering by simply introducing a consistent hashing router. If you don't use Akka, then you'll practically have to redesign your application when the requirement for multiple nodes comes in.

So I'm going to assume that you want to future proof your application, and explain how to use Akka (Akka is a nice way of managing global state anyway even if you don't need multiple nodes).

So in Akka, what you want is an actor that is a stream manager, this will be responsible for creating stream actors if they don't already exist as children of itself. Then the stream actors will be responsible for handling the stream, sending the stream to subscribers, and tracking how many connections are subscribed to them to them, and shutting down when there are no longer any subscribers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top