Sock.js and Erlang - Example

272 views
Skip to first unread message

Jai

unread,
Aug 8, 2012, 1:43:53 PM8/8/12
to soc...@googlegroups.com
I am new to both Sock.js and Erlang. I am trying to run a example.

I have already install Erlang on Mac.
$ erl
Erlang R15B01 (erts-5.9.1) [source] [smp:4:4] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.9.1  (abort with ^G)
1>

I am trying to run a simple echo example from my app.

====rebar.config====
%% -*- erlang -*-

{erl_opts, [debug_info]}. %, fail_on_warning]}.

{deps,
  [
    {sockjs, ".*", {git, "git://github.com/sockjs/sockjs-erlang.git", {branch, "master"}}}
  ]
}.
================

$ ./rebar get-deps
$ ./rebar compile

I am getting following error.

$ ./deps/sockjs/examples/cowboy_echo.erl
escript: exception error: undefined function sockjs_handler:init_state/4
  in function  cowboy_echo:main/1 (./deps/sockjs/examples/cowboy_echo.erl, line 17)
  in call from escript:run/2 (escript.erl, line 727)
  in call from escript:start/1 (escript.erl, line 277)
  in call from init:start_it/1
  in call from init:start_em/1

Are there any pointers that can help me?

Note: I can successfully run this example if I directly use sockjs-erlang rebar.config but I want to run it on my app which describes sockjs as dependency.

Marek Majkowski

unread,
Aug 9, 2012, 5:49:56 AM8/9/12
to j...@vidyamantra.com, soc...@googlegroups.com
Hi!

On Wed, Aug 8, 2012 at 6:43 PM, Jai <j...@vidyamantra.com> wrote:
> I am new to both Sock.js and Erlang. I am trying to run a example.
> $ ./deps/sockjs/examples/cowboy_echo.erl
> escript: exception error: undefined function sockjs_handler:init_state/4
> in function cowboy_echo:main/1 (./deps/sockjs/examples/cowboy_echo.erl,
> line 17)
> in call from escript:run/2 (escript.erl, line 727)
> in call from escript:start/1 (escript.erl, line 277)
> in call from init:start_it/1
> in call from init:start_em/1
>
> Are there any pointers that can help me?

Okay. Welcome to Erlang.

In Erlang, as in other programming languages, programmer is responsible
for pointing to the virtual machine (aka: interpreter) where the program files
are.

By typing:
$ ./deps/sockjs/examples/cowboy_echo.erl

You're saying: dear bash, please run this file.
Bash looks at the header and sees:

#!/usr/bin/env escript

Cool. Escript is executed with this script. Escript then runs erlang
vm. It sees some configuration options in the file:

%%! -smp disable +A1 +K true -pa ebin deps/cowboy/ebin -input

Those are options for the erlang vm. They set, among other
things, the "-pa" path: "-pa ebin deps/cowboy/ebin"

In other words it tells erlang that other runnable files are
in the "ebin" and "deps/cowboy/ebin" directories.

In the traceback you pointed, erlang is complaining that
it can't find the method: "sockjs_handler:init_state"

As you can imagine, sockjs_handler is not in "ebin" nor in "deps/cowboy".
It is in "deps/sockjs/ebin".

What you could do - copy the script and modify "-pa" option to point
to proper dirs.

But here's the problem - using "escript" and hardcoded
directories is not really the idiomatic way on how to
run erlang programs. We're using escript for simplicity
only.

"Properly" running erlang programs is a completely different
topic. You might want to write a proper "otp-ish application",
and so on. Take a look at the epic tutorial from Richard Jones
(where "rebar" is covered):
http://www.metabrew.com/article/erlang-rebar-tutorial-generating-releases-upgrades

As this is a larger topic I suggest reading a book on Erlang.

Marek
Reply all
Reply to author
Forward
0 new messages