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
Oracle Cloud Infrastructure:2026年3月度サービス・アップデート
oracle4engineer
PRO
0
120
会社紹介資料 / Sansan Company Profile
sansan33
PRO
16
410k
フルカイテン株式会社 エンジニア向け採用資料
fullkaiten
0
11k
How to install a gem
indirect
0
1.8k
なぜarray_firstとarray_lastは採用、 array_value_firstとarray_value_lastは 見送りだったか / Why array_value_first and array_value_last was declined, then why array_first and array_last was accpeted?
cocoeyes02
0
160
SSoT(Single Source of Truth)で「壊して再生」する設計
kawauso
2
390
RGBに陥らないために -プロダクトの価値を届けるまで-
righttouch
PRO
0
130
AI時代のIssue駆動開発のススメ
moongift
PRO
0
280
Navigation APIと見るSvelteKitのWeb標準志向
yamanoku
2
130
Blue/Green Deployment を用いた PostgreSQL のメジャーバージョンアップ
kkato1
0
160
「通るまでRe-run」から卒業!落ちないテストを書く勘所
asumikam
2
810
OpenClawでPM業務を自動化
knishioka
1
300
Featured
See All Featured
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.6k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Heart Work Chapter 1 - Part 1
lfama
PRO
5
35k
Building AI with AI
inesmontani
PRO
1
820
Faster Mobile Websites
deanohume
310
31k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
130
SEO for Brand Visibility & Recognition
aleyda
0
4.4k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.8k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.3k
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
150
Optimizing for Happiness
mojombo
378
71k
What does AI have to do with Human Rights?
axbom
PRO
1
2.1k
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