GitHub - sockjs/sockjs-erlang at v0.2.1 (original) (raw)

SockJS family:

SockJS-erlang server

SockJS server written in Erlang. Can run withCowboy orMisultin. SockJS-erlang is compatible withSockJS client version 0.2. Seehttps://github.com/sockjs/sockjs-client for more information on SockJS.

Show me the code!

Cowboy

A simplistic echo SockJS server using Cowboy may look more or less like this:

main(_) -> application:start(sockjs), application:start(cowboy),

SockjsState = sockjs_handler:init_state(
                <<"/echo">>, fun service_echo/2, []),

Routes = [{'_',  [{[<<"echo">>, '...'],
                   sockjs_cowboy_handler, SockjsState}]}],

cowboy:start_listener(http, 100,
                      cowboy_tcp_transport, [{port,     8081}],
                      cowboy_http_protocol, [{dispatch, Routes}]),
receive
    _ -> ok
end.

service_echo(Conn, {recv, Data}) -> sockjs:send(Data, Conn); service_echo(_Conn, _) -> ok.

Misultin

And a simplistic echo SockJS server using Misultin may look more or less like this:

main(_) -> application:start(sockjs),

SockjsState = sockjs_handler:init_state(
                <<"/echo">>, fun service_echo/2, []),

misultin:start_link(
  [{port, 8081}, {autoexit, false}, {ws_autoexit, false},
   {loop,    fun (Req) -> handle_http(Req, SockjsState) end},
   {ws_loop, fun (Req) -> handle_ws(Req, SockjsState) end}]),
receive
    _ -> ok
end.

handle_http(Req, SockjsState) -> case Req:resource([]) of ["echo" | _T] -> sockjs_handler:handle_req(SockjsState, {misultin, Req}); _Any -> Req:respond(404, <<"404 - Nothing here (via sockjs-erlang fallback)\n">>) end.

handle_ws(Req, SockjsState) -> case string:tokens(Req:get(path), "/") of ["echo" | _T] -> sockjs_misultin_handler:handle_ws(SockjsState, {misultin, Req}); false -> closed end.

service_echo(Conn, {recv, Data}) -> sockjs:send(Data, Conn); service_echo(_Conn, _) -> ok.

Dig into the examples directory to get working code:

How to run the examples?

You may need a recent version of Erlang/OTP, at least R14B is recommended.

To run Cowboy example:

cd sockjs-erlang
./rebar -C rebar-cowboy.config get-deps
./rebar -C rebar-cowboy.config compile
./examples/cowboy_echo.erl

To run Misultin example:

cd sockjs-erlang
./rebar -C rebar-misultin.config get-deps
./rebar -C rebar-misultin.config compile
./examples/misultin_echo.erl

This will start a simple /echo SockJS server onhttp://localhost:8081. Open this link in a browser and play around.

SockJS-erlang API

Except for the web framework-specific API's, SockJS-erlang is rather simple. It has just a couple of methods:

The framework-specific calls are more problematic. Instead of trying to explain how to use them, please take a look at the examples.

Stability

SockJS-erlang is quite new, but should be reasonably stable.

Cowboy is passing almost all theSockJS-protocol tests. The one exception is described in this issue:

Misultin is behaving well most of the time, with the exception of a few (mostly websocket related) issues:

Deployment and load balancing

SockJS servers should work well behind many load balancer setups, but it sometimes requres some additional twaks. For more details, please do take a look at the 'Deployment' section inSockJS-node readme.

Development and testing

You need rebar(instructions). Due to a bug in rebar config handling you need a reasonably recent version - newer than late Oct 2011. Alternatively, SockJS-erlang is bundeled with a recent rebar binary.

SockJS-erlang contains a test_server for both Cowboy and Misultin, which is a simple server used for testing.

To run Cowboy test_server:

cd sockjs-erlang
./rebar -C rebar-cowboy.config get-deps
./rebar -C rebar-cowboy.config compile
./examples/cowboy_test_server.erl

To run Misultin test_server:

cd sockjs-erlang
./rebar -C rebar-misultin.config get-deps
./rebar -C rebar-misultin.config compile
./examples/misultin_test_server.erl

That should start test_server on port 8081. Currently, there are two separate test suits using test_server.

SockJS-protocol Python tests

Once test_server is listening on http://localhost:8081 you may test it using SockJS-protocol:

cd sockjs-protocol
make test_deps
./venv/bin/python sockjs-protocol-dev.py

For details seeSockJS-protocol README.

SockJS-client QUnit tests

You need to start a second web server (by default listening on 8080) that is serving various static html and javascript files:

cd sockjs-client
make test

At that point you should have two web servers running: sockjs-erlang on 8081 and sockjs-client on 8080. When you open the browser onhttp://localhost:8080/ you should be able run the QUnit tests against your sockjs-node server.

For details seeSockJS-client README.

Additionally, if you're doing more serious development consider usingmake serve, which will automatically the server when you modify the source code.