Data streaming using streamcontent_from_pid in Yaws/Erlang
<p>I desire to <strong>stream data</strong> with <code>yaws</code> to my comet application, I have read and worked around to understand it but the example from yaws seems to be a little complicated for me (<strong>I am new to Erlang</strong>). I just cannot get my head around...</p>

<p>here is the example from yaws (I modified a little bit):</p>

<pre><code>out(A) -&gt;
    %% Create a random number
    {_A1, A2, A3} = now(),
    random:seed(erlang:phash(node(), 1),
                erlang:phash(A2, A3),
                A3),
    Sz = random:uniform(1),

    Pid = spawn(fun() -&gt;
                        %% Read random junk
                        S="Hello World",
                        P = open_port({spawn, S}, [binary,stream, eof]),
                        rec_loop(A#arg.clisock, P)
                end),

    [{header, {content_length, Sz}},
     {streamcontent_from_pid, "text/html; charset=utf-8", Pid}].


rec_loop(Sock, P) -&gt;
    receive
        {discard, YawsPid} -&gt;
            yaws_api:stream_process_end(Sock, YawsPid);
        {ok, YawsPid} -&gt;
            rec_loop(Sock, YawsPid, P)
    end,
    port_close(P),
    exit(normal).

rec_loop(Sock, YawsPid, P) -&gt;
    receive
        {P, {data, BinData}} -&gt;
            yaws_api:stream_process_deliver(Sock, BinData),
            rec_loop(Sock, YawsPid, P);
        {P, eof} -&gt;
            yaws_api:stream_process_end(Sock, YawsPid)
    end.
</code></pre>

<p><strong>What I need is to transform the above script to which can be combined with the following.</strong></p>

<pre><code>mysql:start_link(p1, "127.0.0.1", "root", "azzkikr", "mydb"),
                {data, Results}  = mysql:fetch(p1, "SELECT*FROM messages WHERE id &gt; " ++ LASTID),
                {mysql_result, FieldNames, FieldValues, NoneA, NoneB} = Results,
                parse_data(FieldValues, [], [], [], [], [])
</code></pre>

<p>Where <code>parse_data(FieldValues, [], [], [], [], [])</code> returns a JSON string of the entry..
<strong>Combined</strong> this script should constantly check for a new entry into database and if there is, it should fetch as comet should.</p>

<p>Thank you, May you all go to paradise!</p>