[erlang-questions] otp / behavior / cowboy_websocket_handler

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


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!


More information about the erlang-questions mailing list