Date post: | 24-May-2015 |
Category: |
Technology |
Upload: | wade-mealing |
View: | 3,179 times |
Download: | 2 times |
Erlang +
Message Queue+
HTML 5 Tech
=
Over-engineered Chat Server
WhatCowboy ?https://github.com/extend/cowboy
Small, fast modular web server written in erlang.
WhatGen Bunny https://github.com/mochi/gen_bunny
RabbitMQ client library for simple pubsub in erlang.
WhatWebsocket ?https://www.websocket.org/
Bi-directional web communication mechanism.
Over Architecturing
You want it.I deliver !
Server
Moving parts
Cowboy
Gen Bunny
Browser
Client
Moving parts
Cowboy
Gen Bunny
Browser
Websockets
TechnicalDetour
Client
Moving parts
BrowserBrowser
Server
HTTP
Port 80Port 80
Client
Moving parts
BrowserBrowser
Server
WS
UpgradeUpgradePort 80Port 80
WHY ?
WORKS WITH EXISTING PROXIES
Client JS
Data is live chat stream
JS Connection
wsUrl = "ws://server:8081/websocket";
websocket = new WebSocket(wsUrl);
Websocket Setupwebsocket = new WebSocket(wsURL);
websocket.onopen = ...
websocket.onmessage =
function(evt){ onMessage(e) };
websocket.onerror = ...
onMessage Handler
function onMessage(e) {
Msg = '<p>' + txt + '</p>'
$('#output').prepend(Msg);
};
Smooth
Yeah
Server SideServer
WS
Cowboy
What comes to mind ?
Kid Rock
Fresh Leather
Pretty Lil' web server
Cowboy
Routes, REST, middleware, web sockets and more.
Cowboy
follows OTP principals.
Cowboy WS
Required Callbacks init websocket_init websocket_handle websocket_info websocket_terminate
Cowboy WS
initUpgrades from http → web socket
Cowboy WS
init({tcp, http}, _Req, _Opts) →{upgrade,protocol,cowboy_websocket}.
Cowboy WS
websocket_initRun on each connection.
Cowboy WS
websocket_init(_Name, Req, _Opts) → Pid = consumer:start_link(), {ok, Req, #state{pid=Pid}}.
Cowboy WS
websocket_handleRecv data from client, optionally return data.
Cowboy WS
websocket_handle(Data, Req, State) → {text, Msg} = Data, {reply, {text, << "recv: ", Msg/binary >>}, Req,
State};
Cowboy WS
websocket_infoLive Push data to client
Cowboy WS
websocket_info(Data, Req, State) → {reply, {text, Data}, Req,State};
Sending to RabbitMQRabbitMQ
websocket_handle
Recv from RabbitMQRabbitMQ
websocket_info
gen_bunnyCONSUMER
Message Queues
gen bunny
gen_bunny
follows OTP principals.
callbacks
Required:● handle_message
● init ● handle_call● handle_cast● handle_info● terminate
callbacks
handle_messageHandle messages from the message subscribed message queue.
callbacks
handle_message(Msg, State) →NewState = act_on_msg(Msg,State){noreply, NewState};
MQ perspectiveEx
chan
ge
Exchange“Fanout”
M S
G
M S
G
M S
G
M S
G
M S
G
M S
G
M S
G
M S
G
M S
G
M S
G
M S
G
M S
G
Live demoWireless Network:Name: rabbitPassword: carrot
VISIT:http://10.1.1.1:8081/
Benchmarking
Method:
Connect to WebsocketWait for signal.Send 100 messages.Receive all messages
Benchmarking
Results:(Didn't get this done in time, sorry)
0.5 1 1.5 2 2.5 3 3.5 4 4.50
2
4
6
8
10
12
Column 1
Column 2
Column 3
Source Codegithub.com/wmeal ing/
bunny-cowboy-websocket