Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

61 lines (41 sloc) 2.1 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, CallbackMod, Options}"),
{p,[], "from the out/1 function, where CallbackMod is an atom identifying your Websocket callback module, and Options can be an empty list."
" This makes the erlang process within yaws processing that particular page do a protocol upgrade from HTTP to the Web Socket Protocol, after which messages received by the server call handle_message in the callback module, and messages can be sent on the Websocket using yaws_api:websocket_send/2."},
{p, [], "The callback module should implement a function handle_message as follows:"},
box("handle_message({Type, Data}) -> HandlerResult
Type :: text|binary
Data :: binary()
HandlerResult :: {reply, {Type, Data}}
| noreply
| {close, Reason}"),
{p, [], "When not replying to a message from a client, you can send messages using"},
box("yaws_api:websocket_send(Pid, {Type, Data})"),
{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.