webbit
play

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


  1. Webbit Evented, single-threaded WebSocket server http://webbitserver.org/ @aslak_hellesoy

  2. Webbit Overview • Single threaded • Non-blocking • High throughput • 1000+ connections • No Servlet API • No XML • 1 Dependency: Netty • 100Kb jar

  3. public static void main(String[] args) { WebServer server = new NettyWebServer(8080); WebServer server.add(new MyHttpHandler()); server.start(); } HttpHandler 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(); } } }

  4. EventSource WebSocket HTTP messages request messages response WebApp WebApp WebApp

  5. 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

  6. 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 };

  7. WebSocket Versions • Hixie-75 • Hixie-76 • Hybi-8 • Hybi-13

  8. Webbit WebSocket API HttpHandler 1..* WebSocketHandler WebSocketConnection

  9. Chat server 100 LOC Java + JavaScript + HTML + CSS

  10. WebSockets lock up your data

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

  12. 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

  13. 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) {};

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

  15. 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

  16. Non-blocking Time-consuming or blocking operations must be offloaded to another thread.

  17. 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"; } } }

  18. 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"; } } }

  19. Designed for Testing • StubRequest • StubResponse • StubConnection • start();stop(); in 1.5 ms

  20. Embedding App Webbit Web Server App

  21. Embedding • Small footprint (0.5 Mb) • Uses very little resources • ~100 kB RAM • Almost no CPU

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

Recommend


More recommend