Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Cowboy - RabbitMQ and websockets
Search
Wade Mealing
September 17, 2013
Technology
3
1.5k
Cowboy - RabbitMQ and websockets
A quick overview on how to plug together some buzzword enabled tech for your enjoyment.
Wade Mealing
September 17, 2013
Tweet
Share
Other Decks in Technology
See All in Technology
Codexとも仲良く。CodeRabbit CLIの紹介
moongift
PRO
1
260
Databricks AI/BI Genie の「値ディクショナリー」をAmazonの奥地(S3)まで見に行く
kameitomohiro
1
340
ソースを読むプロセスの例
sat
PRO
15
9.6k
難しいセキュリティ用語をわかりやすくしてみた
yuta3110
0
340
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
12
81k
いまからでも遅くない!SSL/TLS証明書超入門(It's not too late to start! SSL/TLS Certificates: The Absolute Beginner's Guide)
norimuraz
0
300
GoでもGUIアプリを作りたい!
kworkdev
PRO
0
160
もう外には出ない。より快適なフルリモート環境を目指して
mottyzzz
3
420
Introduction to Bill One Development Engineer
sansan33
PRO
0
300
Click A, Buy B: Rethinking Conversion Attribution in ECommerce Recommendations
lycorptech_jp
PRO
0
110
Dylib Hijacking on macOS: Dead or Alive?
patrickwardle
0
420
HonoとJSXを使って管理画面をサクッと型安全に作ろう
diggymo
0
140
Featured
See All Featured
Making Projects Easy
brettharned
120
6.4k
Leading Effective Engineering Teams in the AI Era
addyosmani
7
530
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Designing Experiences People Love
moore
142
24k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.6k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Code Review Best Practice
trishagee
72
19k
Site-Speed That Sticks
csswizardry
13
910
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
253
22k
GitHub's CSS Performance
jonrohan
1032
470k
Product Roadmaps are Hard
iamctodd
PRO
55
11k
Context Engineering - Making Every Token Count
addyosmani
7
270
Transcript
Erlang + Message Queue + HTML 5 Tech
= Over-engineered Chat Server
What Cowboy ? https://github.com/extend/cowboy Small, fast modular web server written
in erlang.
What Gen Bunny https://github.com/mochi/gen_bunny RabbitMQ client library for simple pubsub
in erlang.
What Websocket ? 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 Technical Detour
Client Moving parts Browser Browser Server HTTP Port 80 Port
80
Client Moving parts Browser Browser Server WS Upgrade Upgrade Port
80 Port 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 Setup websocket = 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 Side Server 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 init Upgrades from http → web socket
Cowboy WS init({tcp, http}, _Req, _Opts) → {upgrade,protocol,cowboy_websocket}.
Cowboy WS websocket_init Run on each connection.
Cowboy WS websocket_init(_Name, Req, _Opts) → Pid = consumer:start_link(), {ok,
Req, #state{pid=Pid}}.
Cowboy WS websocket_handle Recv 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_info Live Push data to client
Cowboy WS websocket_info(Data, Req, State) → {reply, {text, Data}, Req,State};
Sending to RabbitMQ RabbitMQ websocket_handle
Recv from RabbitMQ RabbitMQ websocket_info gen_bunny CONSUMER
Message Queues gen bunny
gen_bunny follows OTP principals.
callbacks Required: • handle_message • init • handle_call • handle_cast
• handle_info • terminate
callbacks handle_message Handle messages from the message subscribed message queue.
callbacks handle_message(Msg, State) → NewState = act_on_msg(Msg,State) {noreply, NewState};
MQ perspective Exchange 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 demo Wireless Network: Name: rabbit Password: carrot VISIT: http://10.1.1.1:8081/
Benchmarking Method: Connect to Websocket Wait 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.5 0 2 4 6 8 10 12 Column 1 Column 2 Column 3
Source Code github.com/wmealing/ bunny-cowboy-websocket