[erlang-questions] otp / behavior / cowboy_websocket_handler

t x <>
Mon Mar 10 22:32:16 CET 2014


  I'm trying to understand what Erlang is doing with the following
piece of code:


  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:

  [ 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:


  And this is a great human-level understanding.

  However, I want to see the code that makes it happen.

  Then, I look at:


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?


More information about the erlang-questions mailing list