title: How do I handle a WebSocket close from the client in Yaws?
body: <p>I have implemented a simple <em>appmod</em> that handle WebSockets and echo back the messages. But how do I handle an <code>ws.close();</code> from the JavaScript client? I have tried with the code below, but <code>handle_message({close, Reason})</code> is never called and <code>ws.onclose = function(evt) {}</code> is never executed on the JavaScript client.</p>
  
  <p>When I use the same JavaScript client code interacting with a node.js websocket, the client receives an <code>onclose</code> event immediately after <code>ws.close();</code>.</p>
  
  <p>Here is the code for my simple <em>appmod</em>:</p>
  
  <pre><code>-module(mywebsocket).
  -export([handle_message/1]).
  
  handle_message({text, Message}) -&gt;
      {reply, {text, &lt;&lt;Message/binary&gt;&gt;}};
  
  handle_message({close, Reason}) -&gt;
      io:format("User closed websocket.~n", []),
      {close, normal}.
  </code></pre>