WebSocket Server and Client implementation in Erlang/Mochiweb
Latest commit 435e2dd Mar 10, 2011 @leandrosilva leandrosilva committed with now path isn't hardcoded
Failed to load latest commit information.
deps/mochiweb initial add Dec 14, 2009
ebin initial add Dec 14, 2009
examples/basic updated websocket client Feb 16, 2010
src now path isn't hardcoded Mar 31, 2011
support initial add Dec 14, 2009
Makefile initial add Dec 14, 2009
README.textile updated docs Feb 16, 2010


Erlang WebSocket Server and Client

WebSocket Server

The server is a simple wrapper around the mochiweb_socket_server to allow WebSocket connections. With this wrapper, you can write WebSocket connection loops just like you’d write a normal mochiweb application. Here’s an example (included with the source code):


-export([start/1, stop/0, loop/1]).

start(Options) ->
    Loop = fun (WebSocket) ->
    mochiweb_websocket:start([{name, ?MODULE}, {loop, Loop} | Options]).

stop() ->

loop(WebSocket) ->
    %% Get the data sent from the client
    Data = WebSocket:get_data(),
    %% For this example...
    %% Of course you could handle JSON or another format of your choice
    case Data of
	%% On initial connect we get this message
	"client-connected" ->
	    WebSocket:send("You are connected!");
	%% Other messages go here
	Other ->
	    Msg = "You Said: " ++ Other,

WebSocket Client

This is a pure Erlang WebSocket client you can use to call a WebSocket Server from Erlang code. For simplicity, the code defines an Erlang Behaviour you implement for your client. The Behaviour defines the functions outlined in the WebSocket API spec. Here’s an example of a simple client:


%% websocket specific callbacks

send(Data) ->

start() ->

%% Handle incoming messages here
onmessage(Data) ->
    io:format("Got some data:: ~p~n",[Data]).

onclose() ->
    io:format("Connection closed~n").

onopen() ->
    io:format("Connection open~n"),

close() ->


Here’s an example of using the client from an erl shell:

> sample:start().
> sample:send("Hello there").
> sample:close().

Check out the examples directory.


  1. Erlang 12.5 or greater
  2. Google Chrome Browser developer channel release

Getting Started:

  1. download the code
  2. CD into the erlang_websocket directory
  3. run make

Run the example:

  1. CD into the examples/basic directory
  2. run the ‘start.sh’ script
  3. Point the Chrome Browser to ‘http://localhost:8000’