Wednesday, September 19, 2012

HTML5 WebSocket using Play Framework 2.0

Recently, I learned about HTML5 WebSockets. It is pretty cool. During the handshake onReady event, I cache the out stream in a static member variable. Another user action that writes to this stream. This simulates a typical order processing scenario, where a customer places order and store owner gets live order notification.

routes
GET / controllers.Application.index()
GET /order controllers.Application.order()

Application.java
package controllers;
import play.*;
import play.libs.F.Callback;
import play.libs.F.Callback0;
import play.mvc.*;
public class Application extends Controller {
  public static WebSocket.Out outCached=null;
  public static Result order() {
  if (outCached!=null)
    outCached.write("Another User Placed an order");
    return ok("Success");
  }
  public static WebSocket index() {
    return new WebSocket() {// Called when the Websocket Handshake is done.
      public void onReady(WebSocket.In in, WebSocket.Out out) {
        outCached=out;// For each event received on the socket,
        in.onMessage(new Callback() {
          public void invoke(String event) {
          Logger.info(event);// Log events to the console
        } 
      });
      in.onClose(new Callback0() {// When the socket is closed.
      public void invoke() {
        outCached=null;
        Logger.info("Disconnected");
      }
    });
    out.write("Hello!");// Send a single 'Hello!' message
    }
  };
}
}
Test Client
I used the WebSocket.org's echo client page in the latest chrome browser. I set the location to ws://localhost:9000.
http://www.websocket.org/echo.html Then place an order from another browser using this link: http://localhost:9000/order
References

Saturday, July 28, 2012

I love stackoverflow.com

I love stackoverlow.com. Easy to find a solution for my problem. Uncluttered user interface. I like their vote up button.

Saturday, July 7, 2012

Sencha's GPL3 licensing too restrictive

After reviewing Sencha's Open Source FAQ, I feel their interpretation of GPL3 licensing is too restrictive. Let us say, I am using Sencha ExtJs to build a public website, then my website source code will become a derived work. If I use the GPL3 license, then I have to distribute the entire source code of my website, including the back-end code. Another common scenario is using ExtJS in an intranet application. Here too, GPL3 license can not be used, as it excludes contractors from accessing the application. It seems like Sencha's GPL licensing will only work for open source projects. All others have go with their commercial license.

Reference

Fiddler Web Debugging Proxy

Today, I came to know about Fiddler. It is a great tool to inspect http requests and responses. I wanted to verify if a REST call is returning the json header  correctly or not. Fiddler came in  handy.
http://www.fiddler2.com/fiddler2/

Friday, July 6, 2012

Install Guice Module


  1. If using,  play 2.0.2, add  "com.typesafe" % "play-plugins-guice" % "2.0.3" to your build.scala file. For play 2.0.1, add  "com.typesafe" % "play-plugins-guice" % "2.0.2" to your build.scala file.
  2. Create a file called play.plugins in your app/conf directory
  3. Add 1500:com.typesafe.plugin.inject.GuicePlugin
  4. Re-run eclipsify command from the play prompt. Initially, I didn't know that I have to do this, and I was struggling for several hours.
  5. Then define Dependency class in module package and inject the service.  That's it

Dependencies.java

package module;
import com.google.inject.*;
import service.*;

public class Dependencies implements Module {
 public void configure(Binder binder) {
     binder.bind(Service.class).to(SomethingService.class);
  }  
}

Application.java


import javax.inject.*;
import service.*;
public class Application extends Controller {  
  @Inject static Service s;
  public static Result index() {
    return ok(index.render(s.demonstrate()));
  }  

Evaluating Logical Expressions

Today, I used Java 6 Scripting API to evaluate logical expressions. Here is an example:
import javax.script.*;
public class EvalScript {
    public static void main(String[] args) throws Exception {
        // create a script engine manager
        ScriptEngineManager factory = new ScriptEngineManager();
        // create a JavaScript engine
        ScriptEngine engine = factory.getEngineByName("JavaScript");
        // evaluate JavaScript code from String
        Boolean result=(Boolean)engine.eval("'sometext'=='sometext'");
                      //result will be true
     }
  }


Reference


Getting Started with Play

Create a java project

  1. Download the latest play 2.0 zip from http://playframework.org
  2. Extract the zip file to a convenient location. Usually, I extract and put it in my c:\, so that it is easily accessible from the command prompt.
  3. Add the play framework path in your PATH environment variable.
  4. Open command prompt and 'cd' to your project folder.
  5. Enter 'play new learn-play' from your project folder (where learn-play is the name of your first project). 
  6. This will prompt for what language template. Pick option 2, which is 'simple java application'
  7. This will create a simple java application template. Look for  'OK, application learn-play is created. Have Fun!' message.

Run your application

  1. Open command prompt and 'cd' to your project folder. Then 'cd' to learn-play folder. 
  2. Run 'play' from the command prompt.
  3. This will show the '[learn-play]$' command prompt.
  4. Enter 'run'
  5. This will run the play application in development mode.
  6. From the browser enter 'http://localhost:9000' will run the application.

Create Eclipse Project

  1. From the play '$' prompt, enter command 'eclipsify'
  2. This will create an eclipse project.
  3. From eclipse, right click and choose Import->Existing Projects into Workspace and browse to your lean-play folder.
  4. This will open your learn-play project.
  5. Whenever you install a new module, you have to re-run the 'eclipsify' command

Reference