Vert.x

Async Data From Cluster to Browser

Chariot DataIO Conference, October 30, 2013

Philadelphia, PA

Lance Ball / @lanceball

What is Vert.x

“Vert.x is a lightweight, high performance application platform for the JVM that's designed for modern mobile, web, and enterprise applications.”
http://vertx.io

What Does That Mean?

Small

Fast

Buzzwordy

Lightweight

“Vert.x is a lightweight, high performance application platform for the JVM...”

< 5MB Download Size

Embeddable

Modular

High Performance

“Vert.x is a lightweight, high performance application platform for the JVM...”

Hundreds of thousands of responses per second

Built on Netty | netty.io

Asynchronous

Back to Work!

Back to Work

~ 500k requests/second

Asynchronous API

  • TCP / HTTP / HTTPS
  • Coming Soon: UDP & DNS
  • File System Operations
  • Event Bus

JVM

“Vert.x is a lightweight, high performance application platform for the JVM...”

There was a time when every new train engine was accompanied by its own rail gauge.

Eventually, having all trains able to run on all track was more important.

-- Robb Greathouse, Red Hat

JVM

“Vert.x is a lightweight, high performance application platform for the JVM...”

Java, JRuby, Jython, Javascript, Groovy, Scala, Clojure

Polygasm

JVM

“Vert.x is a lightweight, high performance application platform for the JVM...”

Each language has an API similar to the core Java API, but exposed with language-specific semantics

JVM

“Vert.x is a lightweight, high performance application platform for the JVM...”

Language APIs are Vert.x modules just like anything else outside of the core Java API

Ada, AWK, BASIC, BBx, Boo, C, COBOL, ColdFusion, Common Lisp, Component Pascal, Erlang, Forth, Go, Haxe, JavaScript, Logo, Lua, Yeti ML, Oberon-2, OCaml, Object Pascal, Pascal, PHP, Prolog, Python, R, REXX, Ruby, Scheme, Tcl

Modern Mobile Enterprise

...for modern mobile, web, and enterprise applications

Modern Mobile Enterprise

Rainbows and Unicorns

© Totally Severe http://www.flickr.com/photos/29364131@N07/
Umm, OK.

Modern Mobile Enterprise

Concurrency With Thread Safety

Asynchronous/Reactive

Client Push

Node.js

Flying Rainbow Unicorns

Some Code



// You can require API modules piecemeal
var container = require('vertx/container');

// Or you can pull everything in at once
// var vertx = require('vertx');
// var container = vertx.container;

var config = {
  "web_root": ".",
  "port": 8080
};

// With vertx-platform you can deploy modules programatically and 
// Vert.x will find, find, download and install them for you at 
// runtime. This is the source code for the presentation I'm giving
// right now.
container.deployModule("io.vertx~mod-web-server~2.0.0-final", config);

					

This is the Verticle that is serving up this presentation.

Even More Code (Java)



// The vertx object is inherited from our superclass
HttpServer server = vertx.createHttpServer();

server.requestHandler(new Handler< HttpServerRequest >() {
    public void handle(HttpServerRequest request) {
        request.response().setStatusCode(779);
        request.response().setStatusMessage("Too many gerbils");
        request.response().write("Off by too many to count error").end();
    }
});

server.listen(8080, "localhost");

					

Javascript



// Requiring 'vertx' pulls in everything
var vertx  = require('vertx');

// And hangs most of it off of a global 'vertx' object
var server = vertx.createHttpServer();

// Handle incoming HTTP requests
server.requestHandler(function(request) {
      request.response.statusCode(779)
      request.response.statusMessage("Too many gerbils")
      request.response.write("Off by too many to count error").end();
});

// Start the server
server.listen(8080, "localhost");

					

Clojure


(ns demo.verticle
  (:require [vertx.http :as http]
            [vertx.stream :as stream]))
 
(defn req-handler [req]
  (-> (http/server-response req {:status-code 779, :status-message "Too many gerbils"})
      (stream/write "Off by too many to count error")
      (http/end)))
 
(-> (http/server)
    (http/on-request req-handler)
    (http/listen 8080))
					

JRuby


require "vertx"

server = Vertx::HttpServer.new

// Handle incoming HTTP requests
server.request_handler do |request|
      request.response.status_code  = 779
      request.response.status_message = "Too many gerbils"
      request.response.write("Off by too many to count error").end;
end

// Start the server
server.listen(8080, "localhost")
					

Verticle

A Verticle is the unit of code that Vert.x runs.


$ vertx run app.js -conf config.json -instances 3
          

Verticle

Verticles can be deployed programmatically too.


# Deploying a Clojure verticle from JRuby!
vertx.deploy_verticle('broker.clj') do |err, deployment_id|
  puts 'deploy successful!' unless err
end
          

Verticle

Verticles are assigned to an event loop

Event loops are assigned to a thread

Threads are assigned to a processor core

Thread safety without compromising cores

Event Loop

DON'T BLOCK THE EVENT LOOP

Event Loop

  • Thread.sleep() Object.wait() CountdownLatch.await() etc..
  • while(true) {console.log("Super Awesome");}
  • Calculating the Nth digit of Π
  • SELECT DISTINCT t1.*, t2.*, t3.* FROM products t1, prices t2, sizes t3 WHERE t1.name LIKE '%bacon%'

Event Loop


var fs = require('vertx/file_system');

fs.open('/etc/passwd', function(buffer) {
  // commit evil
  // It's OK, as long as you DON'T BLOCK THE EVENT LOOP!
});
          

Worker Verticles

Workers
© http://www.flickr.com/photos/stavos52093/9645884201

Worker Verticles

  • Do not run on the event loop
  • Only ever running on a single thread
  • But not always the same thread
  • Can block!

Modules

The unit of deployment in Vert.x. Modules can contain multiple verticles, potentially written in different languages.

API Highlights

Container


var container = require('vertx/container');

container.deployModule("io.vertx~mod-web-server~2.0.0-final", config);
container.deployWorkerVerticle("databaseCruncher.rb", config);
container.deployVerticle("app.clj", config);
          

API Highlights

File System


var fs = require('vertx/file_system');

fs.open('/etc/passwd', function(buffer) {
  // commit evil
  // It's OK, as long as you DON'T BLOCK THE EVENT LOOP!
});
          

API Highlights

HTTP/Net/SSL


var http   = require('vertx/http');
var net    = require('vertx/net');
var server = http.createHttpServer();

server.requestHandler(function(request) {
  request.response.write("Hello from Vert.x!").end();
}).listen(8080, "localhost");

var client = http.createNetClient().connect(8080, function(socket) {
  socket.write("GET / HTTP/1.1\n");
  // etc..
});
          

API Highlights

Timers


var timer = require('vertx/timer');
var console = require('vertx/console');

var id = timer.setTimer(5000, function() {
  console.log("I'm 5 seconds in the future!");
});

timer.setPeriodic(2000, function() {
  console.log("I SHOUT EVERY 2 SECONDS!");
});

console.log("Attention, here comes the loudmouth");
timer.cancelTimer(id);
          

API Highlights

Event Bus


var bus = require('vertx/event_bus');
var console = require('vertx/console');

bus.registerHandler('app.messages', function(msg, reply) {
  console.log("Handler got: " + msg);
  reply("Echoing: " + msg);
});

// send a message to everyone!
bus.publish('app.messages', 'Hey Everybody!');

// send a message to a single recipient and get a response
bus.send('app.messages', 'Hey You!', function(reply) {
  console.log('Got echo: ' + reply);
});
          

Event Bus

  • Message passing between components
  • Scalars & JSON
  • Inter/intra verticle/cluster nodes
  • Point-to-point
  • Request/respond
  • Broadcast

API Highlights

Event Bus


var eb = new vertx.EventBus('http://localhost:8080/eventbus');

eb.onopen = function() {
  eb.registerHandler('app.messages', function(message) {
    console.log('Got a message in the browser! ' 
                + JSON.stringify(message);
  });

  eb.send('app.messages', 
          {type: 'corn chip', flavor: 'nacho cheese', count: 31417});
}
          

Browsers are peers on the Vert.x event bus!

Demo Time

Excercises for the Reader

Security of the mongo data

Minimize duplicate messages (e.g. mongo msg vs. browser msg)

Prevent malicious users from doing bad things

Allow each user to only vote once

Pros / Cons

Resources

http://vertx.io

http://github.com/eclipse/vert.x

http://github.com/lance/dataIO2013

Google vertx group

#vertx on irc.freenode.net

@vertx_project on Twitter

Thanks & Questions