Skip to content
WebSocket Server implementation in Erlang.
Pull request Compare This branch is even with Etsukata:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Erlang_WebSocket_Server is a server-side implementation for the WebSocket Protocol written in Erlang. This library conforms to WebSocket draft-76.The library provides efficient ways of developoing systems which use WebSocket. Handshake process mainly uses the code written by davebryson and ENDOH Takanao.

davebryson: ENDOH Takano:

Process Design Pattern

This library has the following process design pattern.


Each socket sender and reciever are connected to a corresponding WebSocket client. A socket receiver receives WebSocket frames or error message and pass them to the receiver process. The receiver decode passed frames. The handler handles unframed data and errors. The sender frames handled data and sends them to the specific socket sender or broadcast to all except for the socket sender connected to source client. The sender can also send data to all socket senders. All you have to write is what the handler does.

Simple Echo Server

-import(websocket_server, [unicast/2]).
go() ->
   websocket_server:start("localhost", 9000, ?MODULE, default_echo_handler, []).
default_echo_handler() ->
    {message, Data, ConnectionID} -> 
      unicast(Data, ConnectionID),
    _Any -> default_echo_handler()

General Server

-import(websocket_server, [unicast/2, broadcast/2, sendall/1]).

go() ->
   websocket_server:start("localhost", 9000, ?MODULE, general_handler, []).
general_handler() ->
    {open, SocketSenderPid} ->
    %%do something when a connection opend.
    {message, Data, SocketSenderPid} -> 
    %%do something when the handler received data.
    {closed, SocketSenderPid} ->
    %%do something when a connection closed.
    {error, PosixReason, SocketSenderPid}
    %%do something when the handler received an error.


A demonstration of a WebSocket server which uses this library.


Erlang R14B or greater.

Getting Started

Server side: git clone git:// cd erlang_websocket_server/src/ make erl -s websocket_server start Client side: open Chrome browser javascript console var ws = new WebSocket("ws://localhost:9000"); ws.onmessage = function(m) { console.log(m) }; ws.send("hello");

Something went wrong with that request. Please try again.