Webbit Evented, single-threaded WebSocket server - - PowerPoint PPT Presentation

webbit
SMART_READER_LITE
LIVE PREVIEW

Webbit Evented, single-threaded WebSocket server - - PowerPoint PPT Presentation

Webbit Evented, single-threaded WebSocket server http://webbitserver.org/ @aslak_hellesoy Webbit Overview Single threaded Non-blocking High throughput 1000+ connections No Servlet API No XML 1 Dependency: Netty


slide-1
SLIDE 1

Webbit

Evented, single-threaded WebSocket server @aslak_hellesoy

http://webbitserver.org/

slide-2
SLIDE 2
slide-3
SLIDE 3

Webbit Overview

  • Single threaded
  • Non-blocking
  • High throughput
  • 1000+ connections
  • No Servlet API
  • No XML
  • 1 Dependency: Netty
  • 100Kb jar
slide-4
SLIDE 4

HttpHandler WebServer HttpHandler HttpHandler

public class MyHttpHandler implements HttpHandler { public void handleHttpRequest(HttpRequest req, HttpResponse res, HttpControl ctl) { if(req.headers("Accept").contains("text/plain")) { res.header("Content-Type", "text/plain"); res.content("Hello"); res.end(); } else { ctl.nextHandler(); } } }

public static void main(String[] args) { WebServer server = new NettyWebServer(8080); server.add(new MyHttpHandler()); server.start(); }

slide-5
SLIDE 5

WebApp

HTTP

request response

WebApp

WebSocket

messages

WebApp

EventSource

messages

slide-6
SLIDE 6

WebSocket Overview

Web Server

GET /ws HTTP/1.1 Upgrade: websocket Connection: Upgrade Sec-WebSocket-Version: 13 Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw== HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: HSmrc0sMlmm5OPpG2HaGWk= Frames (UTF-8 or binary) can go both ways, asynchronously

slide-7
SLIDE 7

WebSocket in JavaScript

var ws = new WebSocket('ws://my.host/path'); ws.onconnect = function() { ws.send('Hello'); // May be binary }; ws.onmessage = function(e) { console.log(e.data); // May be binary };

slide-8
SLIDE 8

WebSocket Versions

  • Hixie-75
  • Hixie-76
  • Hybi-8
  • Hybi-13
slide-9
SLIDE 9

Webbit WebSocket API

HttpHandler WebSocketHandler WebSocketConnection 1..*

slide-10
SLIDE 10

Chat server 100 LOC Java + JavaScript + HTML + CSS

slide-11
SLIDE 11

WebSockets lock up your data

slide-12
SLIDE 12

If I can’t cURL it it doesn’t exist

slide-13
SLIDE 13

EventSource Overview

Web Server

GET /eventsource HTTP/1.1 Accept: text/eventstream HTTP/1.1 200 OK Content-Type: text/eventstream data: This is a message data: This is a message data: that spans multiple lines

slide-14
SLIDE 14

EventSource in JavaScript

var es = new EventSource('/some/path'); es.onmessage = function(e) { console.log(e.data); }; es.onopen = function(e) {}; es.onerror = function(e) {};

slide-15
SLIDE 15

public class MyEventSourceHandler implements EventSourceHandler { public void onOpen(EventSourceConnection connection) { EventSourceMessage hi = new EventSourceMessage("Hello\nWorld"); connection.send(hi); } public void onClose(EventSourceConnection connection) { } }

EventSource in Webbit

slide-16
SLIDE 16

EventSource Overview

Web Server

GET /eventsource HTTP/1.1 Accept: text/eventstream HTTP/1.1 200 OK Content-Type: text/eventstream event: english data: December 6, 2011 2:08:12 PM EST event: french data: 6 décembre 2011 14:08:12 EST

slide-17
SLIDE 17

Non-blocking

Time-consuming or blocking

  • perations must be offloaded

to another thread.

slide-18
SLIDE 18

public class SlowHttpHandler implements HttpHandler { private final Executor slow = Executors.newCachedThreadPool(); public void handleHttpRequest(HttpRequest req, final HttpResponse res, final HttpControl ctl) { final String result = slowOperation(); res.content(result).end(); } private String slowOperation() { try { Thread.sleep(2000); return "Here you go"; } catch (InterruptedException e) { Thread.currentThread().interrupt(); return "ERROR"; } } }

slide-19
SLIDE 19

public class SlowHttpHandler implements HttpHandler { private final Executor slow = Executors.newCachedThreadPool(); public void handleHttpRequest(HttpRequest req, final HttpResponse res, final HttpControl ctl) { slow.execute(new Runnable() { public void run() { final String result = slowOperation(); ctl.execute(new Runnable() { public void run() { res.content(result).end(); } }); } }); } private String slowOperation() { try { Thread.sleep(2000); return "Here you go"; } catch (InterruptedException e) { Thread.currentThread().interrupt(); return "ERROR"; } } }

slide-20
SLIDE 20

Designed for Testing

  • StubRequest
  • StubResponse
  • StubConnection
  • start();stop(); in 1.5 ms
slide-21
SLIDE 21

Embedding

App

Webbit

Web Server

App

slide-22
SLIDE 22

Embedding

  • Small footprint (0.5 Mb)
  • Uses very little resources
  • ~100 kB RAM
  • Almost no CPU
slide-23
SLIDE 23

Try Webbit

  • http://webbitserver.org
  • REST extension
  • WebSocket RPC extension
  • https://gist.github.com/1421652
  • @aslak_hellesoy