Posts Tagged ‘GuillaumeBort’
[DevoxxFR2013] Live Coding: A WOA Application in 50 Minutes
Lecturer
Guillaume Bort co-founded Zenexity, specializing in Web Oriented Architecture. Previously a J2EE expert, he developed web frameworks for large enterprises, creating Play Framework to prioritize simplicity. He leads Play’s development.
Sadek Drobi, Zenexity’s CTO, focuses on enterprise applications, bridging problem and solution domains. As a programming languages expert, he contributes to Play Framework’s core team.
Abstract
Guillaume Bort and Sadek Drobi’s live coding demonstrates building a Web Oriented Architecture (WOA) application using Play Framework and Scala. Consuming Twitter’s API, they handle JSON, integrate MongoDB, and stream real-time data via Server-Sent Events to an HTML5 interface. The session analyzes reactive programming, asynchronous handling, and scalability, showcasing Play’s efficiency for modern web apps.
Setting Up the Foundation: Play Framework and Twitter API Integration
Bort and Drobi initiate with Play Framework, creating a project via activator. They configure routes for homepage and stream endpoints, using Scala’s async for non-blocking I/O.
Consuming Twitter’s search API: construct URLs with keywords like “DevoxxFR”, include entities for images. Use WS (WebService) for HTTP requests, parsing JSON responses.
They extract tweet data: user, text, images. Handle pagination with since_id for subsequent queries, building a stream of results.
This setup leverages Play’s stateless design, ideal for scalability.
Building Reactive Streams: Enumerators and Asynchronous Processing
To create a continuous stream, they employ Enumerators and Iteratees. Poll Twitter periodically (e.g., every 5 seconds), yielding new tweets.
Code uses concurrent scheduling:
val tweets: Enumerator[Tweet] = Enumerator.generateM {
Future {
// Fetch and parse tweets
Some(newTweets)
}
}
Flatten to a single stream. Handle errors with recover, ensuring resilience.
This reactive approach processes data as it arrives, avoiding blocking and enabling real-time updates.
Persisting Data: Integrating MongoDB with ReactiveMongo
For storage, integrate ReactiveMongo: asynchronous, non-blocking driver. Define Tweet case class, insert via JSONCollection.
val collection = db.collection[JSONCollection]("tweets")
collection.insert(tweetJson)
Query for latest since_id. Use find with sort/take for efficient retrieval.
This maintains asynchrony, aligning with WOA’s distributed nature.
Streaming to Clients: Server-Sent Events and HTML5 Interface
Output as EventSource: chunked response with JSON events.
Ok.chunked(tweets &> EventSource()).as("text/event-stream")
Client-side: JavaScript EventSource listens, appending images with animations.
Handle dynamics: form submission triggers stream, updating UI.
This enables push updates, enhancing interactivity without WebSockets.
Optimizing for Scalability: Backpressure and Error Handling
Address overload: use onBackpressureBuffer to queue, drop, or fail. Custom strategies compress or ignore excess.
Play’s Akka integration aids actor-based concurrency.
Implications: Builds resilient, scalable apps handling high loads gracefully.
Collaborative Development: Live Insights and Community Resources
Session highlights rapid prototyping: zero slides, GitHub commits for following.
Drobi comments on concepts like futures unification in Scala 2.10, inter-library interoperability.
They encourage exploring Play docs, plugins for extensions.
This methodology fosters understanding of reactive paradigms, preparing for distributed systems.
Links:
[DevoxxFR2012] — Rev:2, Add twitter handle
ALTER TABLE speaker ADD COLUMN twitter varchar(255);
These scripts run automatically on startup, supporting continuous delivery.
## RESTful API Development
James Ward constructs JSON endpoints:
public class ApiController extends Controller {
public Result speakers() {
List speakers = Speaker.find.all();
return ok(Json.toJson(speakers));
}
}
Play’s `Json.toJson` uses Jackson for serialization, with configuration in `application.conf`:
play.modules.enabled += “play.modules.reactivemongo.ReactiveMongoModule”
## Form Handling and Validation
Nicolas demonstrates form binding:
public class FormsController extends Controller {
static Form speakerForm = Form.form(Speaker.class);
public Result create() {
Form<Speaker> filled = speakerForm.bindFromRequest();
if (filled.hasErrors()) {
return badRequest(views.html.create.render(filled));
} else {
Speaker speaker = filled.get();
speaker.save();
return redirect(routes.Application.index());
}
}
}
HTML forms use Play’s template engine:
@form(routes.FormsController.create()) {
@inputText(speakerForm(“name”), ‘_label -> “Name”)
@inputText(speakerForm(“twitter”), ‘_label -> “Twitter”)
}
## Frontend Development with HTML5 and JavaScript
Guillaume Bort integrates Twitter Bootstrap and jQuery for responsive design, demonstrating real-time features with WebSockets:
public class StreamController extends Controller {
public WebSocket updates() {
return WebSocket.whenReady((in, out) -> {
// Push updates
});
}
}
## Cloud Deployment to Heroku
James Ward executes deployment:
heroku create devoxx-play-demo
git push heroku master
heroku addons:create heroku-postgresql
Play’s `Procfile` declares the startup command:
web: target/universal/stage/bin/conference-app -Dhttp.port=${PORT}
“`
Implications for Modern Web Development
The presenters conclude that Play 2.0’s integrated toolchain and cloud-ready architecture dramatically accelerate development cycles while producing applications that scale effortlessly in elastic environments.