[erlang-questions] otp / behavior / cowboy_websocket_handler

Loïc Hoguin <>
Tue Mar 11 10:03:37 CET 2014


cowboy_protocol

On 03/11/2014 09:14 AM, t x wrote:
> So I'm still not seeing the "loop"
>
>
> ## https://github.com/extend/cowboy/blob/master/examples/websocket/src/ws_handler.erl
>
>    This file defines websocket_*, thus fulfilling the behavior.
> However, it does not spawn the process itself.
>
>
> ## https://github.com/extend/cowboy/blob/master/examples/websocket/src/websocket_app.erl#L16-L20
>
>    I believe this spawns the process, via calling cowboy:start_http to
> do the actual work.
>
>
> ## https://github.com/extend/cowboy/blob/master/src/cowboy.erl#L43-L46
>
>    This is cowboy:start_http, which appears to just pass the work off
> to ranch:start_listener.
>
>
> ## https://github.com/extend/ranch/blob/master/src/ranch.erl#L39-L68
>
>    This is ranch:start_listener. I have no idea what's going on here.
>
>
> Can someone pin point / explain to me where in ranch this process is
> being spawn?
>
>
> Thanks!
>
>
> PS: I've looked at:
>
> http://ninenines.eu/docs/en/cowboy/HEAD/guide/ws_handlers/
> http://ninenines.eu/docs/en/cowboy/HEAD/manual/cowboy_websocket/
> http://ninenines.eu/docs/en/cowboy/HEAD/manual/cowboy_websocket_handler/
>
> However, I want more details / see the mechanics of how it works / get
> familiar with tracing through Erlang code.
>
> On Tue, Mar 11, 2014 at 12:37 AM, Loïc Hoguin <> wrote:
>> The actual code is in cowboy_websocket. (Hence init/3 returning it wants to
>> upgrade to cowboy_websocket).
>>
>> The guide now has a proper chapter for Websocket (on github) that explains
>> that part of the code.
>>
>>
>> On 03/10/2014 10:32 PM, t x wrote:
>>>
>>> Hi,
>>>
>>>     I'm trying to understand what Erlang is doing with the following
>>> piece of code:
>>>
>>>
>>> https://github.com/extend/cowboy/blob/master/examples/websocket/src/ws_handler.erl
>>>
>>>     Please note:
>>>
>>>     * this code is running fine (I don't need help trouble shooting the
>>> code)
>>>
>>>     * the behavior I get matches my intuition of what is happening
>>>
>>>     My actual problem is _why_ does everything happen the way it is.
>>>
>>>
>>> ### Behavior I get:
>>>
>>>     Upon opening a websocket and connecting, I get back:
>>>
>>>     Hello!
>>>     [ 1 second delay ]
>>>     How' you doing'?
>>>     [ 1 second delay ]
>>>     How' you doing'?
>>>     ...
>>>
>>>
>>>     This behavior is expected because
>>>
>>>     * websocket_init is called, which causes a "Hello!" message to be
>>> sent to self (handled by websocket_info)
>>>
>>>     * every time websocket_info is called, it:
>>>         * registers 1-second call back for "How'you doing'?"
>>>         * and sends out the msg it received
>>>
>>>
>>> ## Question: What is happening behind the scenes with:
>>>
>>>     -behavior(cowboy_websocket_handler) which causes all this to happen?
>>>
>>>
>>>
>>> ## What I've tried:
>>>
>>>     So I googled and got:
>>>
>>>
>>> https://github.com/extend/cowboy/blob/master/manual/cowboy_websocket_handler.md
>>>
>>>     And this is a great human-level understanding.
>>>
>>>
>>>     However, I want to see the code that makes it happen.
>>>
>>>
>>>     Then, I look at:
>>>
>>>
>>> https://github.com/extend/cowboy/blob/master/src/cowboy_websocket_handler.erl
>>>
>>>
>>>
>>> And now, I am confused.
>>>
>>>
>>>     What I see is a bunch of "-callback" lines.
>>>
>>>
>>>
>>>     What I expect to see is something like:
>>>
>>>     spawn(func, ...)
>>>
>>>     func ( ... ) ->
>>>        some loop or some sort for handling messages
>>>
>>>
>>>     In particular, my confusion is: it's clear that
>>> behavior(cowboy_websocket_handler) causes some process to be spawned
>>> somewhere. Where is this happening?
>>>
>>>
>>>
>>> Thanks!
>>> _______________________________________________
>>> erlang-questions mailing list
>>> 
>>> http://erlang.org/mailman/listinfo/erlang-questions
>>>
>>
>> --
>> Loïc Hoguin
>> http://ninenines.eu

-- 
Loïc Hoguin
http://ninenines.eu



More information about the erlang-questions mailing list