title: WebSockets in Relation with TCP/IP Sockets on Misultin Erlang HTTP Library
body: <p>i must say that i am impressed by <b><a href="https://github.com/ostinelli/misultin" rel="nofollow">Misultin's</a></b> support for Web Sockets (some examples <b><a href="https://github.com/ostinelli/misultin/wiki/ExamplesPage" rel="nofollow">here</a></b>). My JavaScript is firing requests and getting responses down the wire with "negligible" delay or lag, Great !!</p>
  
  <p>Looking at how the data handler loop for WebSockets looks like, it resembles that of normal TCP/IP Sockets, atleast the basic way in Erlang</p>
  
  <pre>
  % callback on received websockets data
  handle_websocket(Ws) ->
      receive
          {browser, Data} ->
              Ws:send(["received '", Data, "'"]),
              handle_websocket(Ws);
          _Ignore ->
              handle_websocket(Ws)
      after 5000 ->
          Ws:send("pushing!"),
          handle_websocket(Ws)
      end.
  </pre>
  
  <p>This piece of code is executed in a process which is spawned by Misultin, a function you give to it while starting your server like this below:
  <pre>
  start(Port)-&gt;
      HTTPHandler = fun(Req) -&gt; handle_http(Req, Port) end,
      WebSocketHandler = fun(Ws) -&gt; <b>handle_websocket(Ws)</b> end,
      Options = [{port, Port},{loop, HTTPHandler},{ws_loop, WebSocketHandler}],
      misultin:start_link(Options).
  </pre>. More Code about this, check out the example page.<br>I have several questions.<br><br>
  <b>Question 1:</b> Can i change the controlling Process of a Web Socket as we normally do with the TCP/IP Sockets in Erlang ? (we normally use: <b><a href="http://www.erlang.org/doc/man/gen_tcp.html#controlling_process-2" rel="nofollow"><code>gen_tcp:controlling_process(Socket,NewProcessId)</code></a></b>) <br>
  <b>Question 2:</b> Is <a href="https://github.com/ostinelli/misultin/wiki/_pages" rel="nofollow">Misultin</a> the only Erlang/OTP HTTP library which supports WebSockets ? Where are the rest ?
  <hr><b>EDIT : </b>
  <b>Now, the reason why i need to be able to transfer the WebSocket control from Misultin</b><br>
  Think of a <code>gen_server</code> that will control a pool of WebSockets, say its a game Server. In the current Misultin Example, for every WebSocket Connection, there is a controlling process, in other-words for every WebSocket, there will be a spawned process. Now, i know Erlang is a hero with Processes but, i do not want this, i want these initial processes to die as soon as they handle over to my <code>gen_server</code> the control authority of the WebSocket.<br>
  I would want this <code>gen_server</code> to switch data amongst these WebSockets. In the current implementation, i need to keep track of the <code>Pid</code> of the Misultin handle_websocket process like this:</p>
  
  <pre>
  
  %% Here is misultin's control process
  %% I get its Pid and save it somewhere
  %% and link it to my_gen_server so that 
  %% if it exits i know its gone
  
  handle_websocket(Ws)-&gt;
      process_flag(trap_exit,true),
      Pid = self(),
      link(my_gen_server),
      save_connection(Pid),
      wait_msgs(Ws).
  
  wait_msgs(Ws)-&gt;
      receive
          {browser,Data}-&gt;
              FromPid = self(),
              send_to_gen_server(Data,FromPid),
              handle_websocket(Ws);
          {broadcast,Message} -&gt;
              %% i can broadcast to all connected WebSockets      
              Ws:send(Message),
              handle_websocket(Ws);
          _Ignore -&gt; handle_websocket(Ws)  
      end.
  </pre>
  
  <p>Above, the idea works very well, whereby i save all controlling process into Mnesia Ram Table and look it up against a given criteria if the application wants to send to that particular user a message. However, with what i want to achieve, i realise that in the real-world, the processes may be so many that my server may crash. I want atleast one <code>gen_server</code> to control thousands of the Web Sockets than having a process for each Web Socket, in this way, i could some how conserve memory.<br><br>
  <b>Suggestion:</b> Misultin's Author could create Web Socket Groups implementation for us in his next release, whereby we can have a group of WebSockets controlled by the same process. This would be similar to <code>Nitrogen's Comet Groups</code> in which comet connections are grouped together under the same control. If this aint possible, we will need the control ourselves, provide an API where we can take over the control of these Web Sockets.
  <br><br><b> What do you Engineers think about this ?<br> What is your suggestion and/or Comment about this ? </b><br> Misultin's Author could say something about this. Thanks to all</p>