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.7k
Elixir/PhoenixによるWeb開発の現場から
ohr486
1
480
Hacking Phoenix Performance
ohr486
1
340
Plug & WAF
ohr486
2
490
elixirをプロダクションに導入する
ohr486
1
630
IEx maniacs
ohr486
4
600
Hack and Read Elixir
ohr486
2
720
Running App on AppRunner
ohr486
0
780
sponsor-talk-drecom-heisei-ruby-kaigi
ohr486
0
830
Other Decks in Programming
See All in Programming
AIネイティブなプロダクトをGolangで挑む取り組み
nmatsumoto4
0
120
GraphRAGの仕組みまるわかり
tosuri13
7
430
Using AI Tools Around Software Development
inouehi
0
1.2k
単体テストの始め方/作り方
toms74209200
0
490
関数型まつりレポート for JuliaTokai #22
antimon2
0
120
Development of an App for Intuitive AI Learning - Blockly Summit 2025
teba_eleven
0
120
関数型まつり2025登壇資料「関数プログラミングと再帰」
taisontsukada
2
830
AWS CDKの推しポイント 〜CloudFormationと比較してみた〜
akihisaikeda
3
280
実践ArchUnit ~実例による検証パターンの紹介~
ogiwarat
2
270
Perplexity Slack Botを作ってAI活用を進めた話 / AI Engineering Summit プレイベント
n3xem
0
660
The Evolution of Enterprise Java with Jakarta EE 11 and Beyond
ivargrimstad
1
800
レガシーシステムの機能調査・開発におけるAI利活用
takuya_ohtonari
0
600
Featured
See All Featured
VelocityConf: Rendering Performance Case Studies
addyosmani
329
24k
Practical Orchestrator
shlominoach
188
11k
Why You Should Never Use an ORM
jnunemaker
PRO
56
9.4k
Stop Working from a Prison Cell
hatefulcrawdad
269
20k
Large-scale JavaScript Application Architecture
addyosmani
512
110k
Reflections from 52 weeks, 52 projects
jeffersonlam
351
20k
The Straight Up "How To Draw Better" Workshop
denniskardys
233
140k
Optimizing for Happiness
mojombo
379
70k
Fireside Chat
paigeccino
37
3.5k
Into the Great Unknown - MozCon
thekraken
39
1.8k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
Statistics for Hackers
jakevdp
799
220k
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