Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…
Cannot retrieve contributors at this time
87 lines (58 sloc) 3.17 KB
box(Str) ->
tbox(T) ->
ssi(File) ->
{ssi, File,[],[]}}}.
out(A) ->
[{ssi, "", "%%",[{"websockets", "choosen"}]},
{'div',[{id, "entry"}],
[{h1, [], "Web Sockets in Yaws"},
{p, [], ["Web Sockets! The new kid in town! Joe ", {a, [{href, ""}], "loves it"}, ", maybe you should too?"]},
{p, [], "Web Sockets allow for *real* two-way communication between the browser and Yaws without the overhead and latency that come with polling/long-polling solutions. That should be enough for an introduction. Now... how to use it?"},
{p, [], "First start by returning:"},
box(" {websocket, OwnerPid, SocketMode}"),
{p,[], "from the out/1 function."
" This makes the erlang process within yaws processing that particular page do a protocol upgrade from HTTP to the Web Socket Protocol, after which the OwnerPid can use the socket to interface directly with the Web Sockets client."},
{p,[], "When yaws completes the Web Sockets handshake, it sends one of the following messages to OwnerPid:"},
[{li,[]," {ok, WebSocket} indicates to OwnerPid that the handshake completed successfully and that the WebSocket is set up and can be used to communicate with the web sockets client."},
{li,[]," discard indicates to OwnerPid that the handshake failed and no web sockets connection is available."}]},
{p,[], "SocketMode defines how the messages sent by the client are to be delivered to the OwnerPid."},
{ul, [], [
{li, [], [
{p,[], "On passive mode (SocketMode=false) it is up to the receiving process to issue a synchronous request to: "},
{p, [], " to grab the incoming messages."}
{li, [], [
{p, [], "On active mode (SocketMode=true) the incoming Web Socket data frames are delivered to OwnerPid as messages. On this mode the following messages are to be expected:"},
box("{tcp, WebSocket, DataFrame}
{tcp_closed, WebSocket}"),
{p, [], "To extract the data from a Web Socket data frame you can use this function: "},
{li, [], [
{p, [], "If SocketMode=once then only ONE message will be sent as if in active mode and after that we're back to passive mode."}
{p,[], "For switching between the various \"receive modes\" you can do this:"},
box("yaws_api:websocket_setopts(WebSocket, [{active, NewSocketMode}])"),
"This function just wraps (gen_tcp|ssl):setops/2 to absctract away from using a regular/secure http connection."},
{p, [], "Enough theory for now. Sample echo server follows!"},
{p, [],
["The above code can be executed ",
{a, [{href, "websockets_example.yaws"}], "Here"},
{ssi, "END2",[],[]}
Jump to Line
Something went wrong with that request. Please try again.