Step-by-Step for a Simple Twitter with Play Framework, AJAX, CRUD and Heroku

On August 31st, 2011: I will have the great honor to present our beloved Play Framework at Salesforce’s Dreamforce conference here in San Francisco, CA. If you are attending, please feel free to check it out or just stop by to say hello. Thank you Zenexity for the opportunity! I am always excited to help spread the word on this awesome framework!

So the big announcement is out—Heroku started offering native support to Play Framework applications! If you haven’t heard it, checkout the post from Jesper Joergensen on Heroku’s blog.

So for the presentation, I am setting up a very basic Twitter clone; it’s meant to be simple yet it displays just enough of the productivity that Play! provides. I am gonna go through the steps to setup the demo application which should cover what was announced on Heroku’s blog post but with a little more depth.

  1. First step let’s create the application
    play new twitter

  2. Add dependency to CRUD module (conf/dependencies.yml)
    – play -> crud

  3. Get the dependencies
    play dependencies

  4. IDE Integration
    play eclipsify (for Eclipse), play idealize (for IntelliJ) or play netbeansify (for Netbeans).

  5. Create Model (app/models/Tweet.java)
    package models;

import java.util.Date;
import java.util.List;

import javax.persistence.Entity;

import play.data.validation.MaxSize;
import play.data.validation.Required;
import play.db.jpa.Model;

@Entity
public class Tweet extends Model {

@Required
@MaxSize(140)
public String tweet;

@Required
public Date createDate = new Date();

public static List findLatest() {
return Tweet.find(“order by createDate desc”).fetch();
}

@Override
public String toString() {
return this.tweet;
}

}

  1. Define DB for JPA Models (conf/application.conf)
    db=${DATABASE_URL}

  2. Add Controller Actions (app/controllers/Application.java)
    package controllers;

import java.util.List;

import models.Tweet;
import play.mvc.Controller;

public class Application extends Controller {

public static void index() {
List tweets = Tweet.findLatest();
render(tweets);
}

public static void create(String msg) {
Tweet tweet = new Tweet();
tweet.tweet = msg;
tweet.save();
render(tweet);
}

public static void tweets() {
List tweets = Tweet.findLatest();
renderJSON(tweets);
}
}

  1. Define Main View (app/views/Application/index.html)

{extends ‘main.html’ /}

#{set title:’Home’ /}



    #{list tweets}
  • ${.tweet} (${.createDate.since()})

  • #{/list}



  1. Define Create Action View (app/views/Application/create.html)

  2. ${tweet.tweet} (${tweet.createDate.since()})
  3. Create Unit Test for Tweet Model
    import models.Tweet;

import org.junit.Assert;
import org.junit.Test;

import play.test.UnitTest;

public class TweetTest extends UnitTest {

@Test
public void testModelSave() {
long count = Tweet.count();
Tweet t = new Tweet();
t.tweet = “my sample tweet”;
t.save();
long count2 = Tweet.count();
Assert.assertEquals(count + 1, count2);
}

}

  1. Create CRUD Admin for Tweet Model
    package controllers;
    public class Tweets extends CRUD {
    }

  2. Add Routes (conf/routes)

  3. /admin module:crud

GET /rest/tweets Application.tweets

  1. Define Messages for CRUD Admin (conf/messages)
    tweet=Tweet
    createDate=Date Created

  2. Define Procfile
    web: play run –%$FRAMEWORKID –http.port=$PORT -DusePrecompiled=$USEPRECOMPILED -DDATABASE_URL=mem

  3. Run in Development
    play run –%dev -DusePrecompiled=false -DDATABASE_URL=mem

  4. Create Application on Heroku
    heroku create play-twitter –stack cedar

  5. Setup Git Repository
    git init; git add .; git commit -a -m “Initial Commit”; git remote add heroku git@heroku.com:play-twitter.git

  6. Setup Heroku Environment Variables
    heroku config:add FRAMEWORKID=prod; heroku config:add USEPRECOMPILED=true

  7. Deploy to Heroku
    git push heroku master

  8. If anything went wrong you can always check the log
    heroku logs

  9. To Setup a Real Database on Heroku
    heroku addons:add shared-database

You can checkout a live demo here, the admin interface here or clone the source code on Github.

Voila! Now Go Play!