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
ErlangElixirFest2018-ohr486-session
Search
ohr486
June 16, 2018
Programming
5
1.7k
ErlangElixirFest2018-ohr486-session
We love PubSub.
ohr486
June 16, 2018
Tweet
Share
More Decks by ohr486
See All by ohr486
負荷試験Night#1 負荷試験2023年トレンド
ohr486
17
4.6k
Elixir/PhoenixによるWeb開発の現場から
ohr486
1
430
Hacking Phoenix Performance
ohr486
1
310
Plug & WAF
ohr486
2
460
elixirをプロダクションに導入する
ohr486
1
610
IEx maniacs
ohr486
4
560
Hack and Read Elixir
ohr486
2
690
Running App on AppRunner
ohr486
0
730
sponsor-talk-drecom-heisei-ruby-kaigi
ohr486
0
800
Other Decks in Programming
See All in Programming
Rubyと自由とAIと
yotii23
6
1.9k
AWS Step Functions は CDK で書こう!
konokenj
4
800
仕様変更に耐えるための"今の"DRY原則を考える
mkmk884
9
3.2k
Honoとフロントエンドの 型安全性について
yodaka
7
1.5k
Rubyで始める関数型ドメインモデリング
shogo_tksk
0
140
「個人開発マネタイズ大全」が教えてくれたこと
bani24884
1
280
Webフレームワークとともに利用するWeb components / JSConf.jp おかわり
spring_raining
1
130
PHPのバージョンアップ時にも役立ったAST
matsuo_atsushi
0
230
GoとPHPのインターフェイスの違い
shimabox
2
210
Boos Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
540
Datadog Workflow Automation で圧倒的価値提供
showwin
1
300
生成AIで加速するテスト実装 - ロリポップ for Gamersの事例と 生成AIエディタの活用
kinosuke01
0
140
Featured
See All Featured
Agile that works and the tools we love
rasmusluckow
328
21k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
The Power of CSS Pseudo Elements
geoffreycrofte
75
5.5k
Stop Working from a Prison Cell
hatefulcrawdad
268
20k
YesSQL, Process and Tooling at Scale
rocio
172
14k
Writing Fast Ruby
sferik
628
61k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
227
22k
Embracing the Ebb and Flow
colly
84
4.6k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
27
1.6k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.7k
Transcript
Channel先生、 PubSubが・・・ したいです・・・! Erlang&Elixir Fest 2018 おーはら
About Me おーはら Twitter: @ohrdev GitHub: ohr486 FB: tsunenori.oohara Work:
Elixir/Ruby/Infra Engneer Fav: PubSub, 仏像製作
ハンズオンイベント • 5F ◦ 5B-1, 5B-2 • https://github.com/ohr486/ErlangElixirFestHandsO n/wiki •
Enum, Stream, Flow を使ったデータ処理 ◦ Eager ◦ Lazy ◦ Concurrent
発表のテーマ Erlang in Anger PubSub in Channel
PubSubしない Phoenixはた だのRailsだ
発表のテーマ Phoenix Channel & PubSub
Agenda • 今日のゴール • Phoenix Channel • Phoenix PubSub •
PubSub Backend • PubSub Adapterの作り方 • まとめ
今日のゴール • PhoenixのChannelの構造を知る • Phoenix.PubSubの構造を知る • PubSub Backendについて知る • カスタムAdapterの作り方を理解する
• PubSub力を上げる
5min Phoenix Channel App • Phoenix Channelによるチャット作成(よくあるサンプル) ◦ https://github.com/chrismccord/phoenix_chat_example •
step.1 : Phoenixプロジェクトの作成 • step.2 : Socketハンドラ設定 [ server ] • step.3 : Channelモジュール作成 [ server ] • step.4 : Client実装 [ client ]
Phoenix Channel • Phoenix Channelは、Phoenixフレームワークが提供する、PubSubモデルのメッ セージ通信の機構 • メッセージ受信 : トピックを購読していればメッセージを受信
• メッセージ送信 : トピックに対してメッセージをブロードキャスト • クライアントは、メッセージの受信・送信どちらも可能 Phoenix Channel Topic1 Topic2 購読 送信 受信 client1 client2 client3
Phoenix Channel • クライアントとサーバーの通信は、WebSocketを通して行われます • クライアントとサーバーのWebScoket接続は1つです • WebSocketの上に複数のチャネルソケットがある 単一のwebsocket接続 Phoenix
Server client channel1の通信 channel2の通信
Phoenix Channel Phoenix Server VM socket handler client1 chnnel [client1,topic1]
socket handler client2 chnnel [client2,topic1] chnnel [client1,topic2] PubSub PubSub Backend ・チャネルへのルーティング ・メッセージのディスパッチ ・トピックの購読 ・トピックの退会 ・メッセージ送受信 ・認証 Phoenix.Socket Phoenix.Channel Phoenix.PubSub websocket websocket
Phoenix PubSub • PhoenixのPubSub層は、PubSubモジュール、アダプタ、バックエンドの3層に分解 できます • PubSubは以下の2種類に分類できます ◦ 自サーバーのチャネルに対して通信を行うローカル PubSub
◦ 同一クラスタ内の他サーバーのチャネルに対して通信を行うリモート PubSub client1 client2 client3 local pubsub channel1 channel2 remote pubsub local pubsub remote pubsub channel3 PubSub Backend Phoenix.PubSub Phoenix.PubSub.Local PubSub Adapter
Phoenix PubSub application master MyApp.Supervisor MyApp.Web.Endpoint MyApp.PubSub.Supervisor PubSub.LocalSupervisor MyApp.PubSub.Endpoint.Server Supervisor.Default
PubSub.GC Local PubSub Process Remote PubSub Process TCP Listener Supervisor TCP Acceptor Supervisor … Conn Supervisor … … TCP Acceptor Channel Process PubSub Channel Controller WebSocket Handler
PubSub Backend • PhoenixはPubSubのバックエンドを選択できます • Official Backend Adapter ◦ PG2
( Process Group 2 ) ◦ Redis • 3rd Party Backend Adapter ◦ https://github.com/h4cc/awesome-elixir#framework-components • Custom Backend ◦ Adapterを実装することで、独自のバックエンドを利用できます ◦ see: LT@cctiger36 (phoenix_pubsub_redis_z) ▪ 複数のRedisをbackendにするpubsub adapter
PubSub Backend • PubSub Adapterを変更する事で、バックエンドを差し替え可能です • Adapterの変更方法 ◦ configのpubsub adapterを変更
{phoenix-app}/config/config.exs
PubSub Adapterの作り方 • PubSub Adapterとは ◦ (1) LocalSupervisor、(2) Backendとの通信を行うGenServerプロセス を持つSupervisorモジュー ル
• AdapterのRemotePubSubProcessの挙動 ◦ Backendとの通信を行う ◦ Backendからの通信を受け取り、 Local PubSubにブロードキャスト ◦ Local PubSubからメッセージを受信し、 Backendに送信
PubSub Adapterの作り方 application master MyApp.Supervisor MyApp.Web.Endpoint MyApp.PubSub.Supervisor PubSub.LocalSupervisor MyApp.PubSub.Endpoint.Server Supervisor.Default
PubSub.GC Local PubSub Process Remote PubSub Process TCP Listener Supervisor TCP Acceptor Supervisor … Conn Supervisor … … TCP Acceptor Channel Process WebSocket Handler Backend Driver Process Adapter
PubSub Adapterの作り方 supervisorとして振る舞う LocalSupervisorをsuperviseする Local PubSubの振る舞いを定義 Remote PubSubの実体 local pubsub
local pubsub supervisor remote pubsub pubsub adapter
PubSub Adapterの作り方 serverとして振る舞う Local PubSubに対するブロードキャスト Backendに対するメッセージ送信 local pubsub local pubsub
supervisor remote pubsub pubsub adapter
まとめ • PhoenixのChannelのアーキテクチャを紹介しました • PhoenixにおけるPubSubの構造を紹介しました • カスタムBackendのAdapterの作り方を紹介しました
Let’s PubSub
powered by