Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Yaws and websockets to subscribe to data
    primarykey
    data
    text
    <p>I have a gen_server speaking to several hardware sensors. This setup works fine. Now I want to extend this system with a real time visualization of the data. And I want to deliver this data to a web client over websockets.</p> <p>As I might have several "listeners", e.g. several people visualizing this data on different web browsers, I am thinking of something resembling the Observer Pattern. Each time a web client asks to subscribe to a sensor, it should be added to a list of stakeholders for that sensor. As soon as new sensor data arrives, it should be pushed out to the client without delay.</p> <p>I am using yaws to quickly get websocket functionality. My problem is related to the way that yaws seems to work. On the server side I only "see" the client at connection time through the <code>A#arg.clisock</code> value (e.g. <code>#Port&lt;0.2825&gt;</code>). In the code below I register <code>ws_server</code> to receive the callbacks when new data enters from the client. After this point yaws seems to only allow me to respond to messages entering server side.</p> <pre><code>out(A) -&gt; CallbackMod = ws_server, Opts = [{origin, "http://" ++ (A#arg.headers)#headers.host}], {websocket, CallbackMod, Opts}. </code></pre> <p>This is what the callback module looks like:</p> <pre><code>% handle_message(Incoming) % Incoming :: {text,Msg} | {binary,Msg} | {close, Status, Reason} handle_message({Type,Data}) -&gt; gen_server:cast(?SERVER,{websocket,Data}), noreply. </code></pre> <p>Nowhere, it seems, am I able to react to a message such as <code>subscribe &lt;sensor&gt;</code> and (after connection time) dynamically add this stakeholder to a list of observers.</p> <p>How can I use the yaws server to accomplish asynchronous pushing of data to client <em>and</em> during session add and remove sensors that I want to listen to. Basically the easiest way would be if yaws could call back <code>handle_message/2</code> with first argument being <code>From</code>. Otherwise I need to add a ref to keep a ref on both sides and send that to server each time, it seems backwards that I need to keep that information.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload