Wynn Netherland changelog.com/posts

webbit: Evented HTTP, WebSocket server for Java and Clojure

Evented HTTP web apps are hot. Joe Walnes has created a Java spin on the idea with Webbit. Webbit makes it super simple to create a WebSocket server in just a few lines of Java:

public class HelloWebSockets implements WebSocketHandler {

  int connectionCount;

  public void onOpen(WebSocketConnection connection) {
    connection.send("Hello! There are " + connectionCount + " other connections active");

  public void onClose(WebSocketConnection connection) {

  public void onMessage(WebSocketConnection connection, String message) {
    connection.send(message.toUpperCase()); // echo back message in upper case

  public static void main(String args) {
    WebServer webServer = WebServers.createWebServer(8080)
      .add("/hellowebsocket", new HelloWebSockets())
      .add(new StaticFileHandler("/web"))
    System.out.println("Server running at " + webServer.getUri());

Webbit isn’t just for Java folk. Jay Fields has a post demonstrating using Webbit in Clojure:

(ns server
  (:require [clojure.contrib.json :as json]
            [clojure.string :as s])
  (:import [webbit WebServer WebServers WebSocketHandler]
           [webbit.handler StaticFileHandler]))

(defn on-message [connection json-message]
  (let [message (-> json-message json/read-json (get-in [:data :message]))]
    (.send connection (json/json-str {:type "upcased" :message (s/upper-case message) }))))

(doto (WebServers/createWebServer 8080)
  (.add "/websocket"
        (proxy [WebSocketHandler] []
          (onOpen [c] (println "opened" c))
          (onClose [c] (println "closed" c))
          (onMessage [c j] (on-message c j))))

  (.add (StaticFileHandler. "."))

(println "server up")

Check out the Webbit Readme or the samples folder for more information. We’ll definitely be watching Webbit.

[Source on GitHub]


Sign in or Join to comment or subscribe

Player art
  0:00 / 0:00