Reputation: 829
I am learning erlang and I tried this project - https://github.com/hcs42/cowboy_tutorial_webchat.
I build it using
./rebar compile generate skip-deps=true && rel/chat/bin/chat console
In case of LAMP, if I would change .js file, I would only reload page and new changes would be applied.
In case of this erlang project, everytime I change something in .js file I must recompile and generate using rebat. Why is that so?
Also, if I look at the JS implementation, message is sent using
socket.send(message);
and is received on server side as:
websocket_handle({text, Msg}, Req, State) ->
{reply, {text, << "You said: ", Msg/binary >>}, Req, State};
How does this work, if cowboy accepts {text,Msg},Req,State but client send only text? Also client never gets {reply,{text,YouSaid...}, but only gets plain text message?
Upvotes: 1
Views: 51
Reputation: 20024
Regarding rebuilds, this project is generating an Erlang release, which is a directory structure holding a complete system needed to run your application. Every time you run rebar generate
, it recreates the release, making a copy of the static files and using that copy. If you were building the project in-place (more common for development), then Cowboy would serve the files directly from your source tree and you would be able to pick up new changes without rebuilding the server.
As for the {text,Msg}
and {reply, ...}
tuples, these are created by the underlying cowboy websockets library code. It receives client data from the socket, creates the {text, Msg}
tuple and the Req
variables based on what it received, and then calls websocket_handle/3
with those values and the State
variable. When that handler function returns a {reply, ...}
tuple, the underlying websockets code extracts the message from it and sends it back over the socket to the client. You can find more information about the websocket_handle/3
function in the cowboy_websocket_handler documentation.
Upvotes: 2