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
510
Hacking Phoenix Performance
ohr486
1
350
Plug & WAF
ohr486
2
500
elixirをプロダクションに導入する
ohr486
1
650
IEx maniacs
ohr486
4
610
Hack and Read Elixir
ohr486
2
730
Running App on AppRunner
ohr486
0
800
sponsor-talk-drecom-heisei-ruby-kaigi
ohr486
0
850
Other Decks in Programming
See All in Programming
State of CSS 2025
benjaminkott
1
110
技術的負債で信頼性が限界だったWordPress運用をShifterで完全復活させた話
rvirus0817
1
2k
Kiroの仕様駆動開発から見えてきたAIコーディングとの正しい付き合い方
clshinji
1
120
新しいモバイルアプリ勉強会(仮)について
uetyo
1
260
開発チーム・開発組織の設計改善スキルの向上
masuda220
PRO
4
700
[FEConf 2025] 모노레포 절망편, 14개 레포로 부활하기까지 걸린 1년
mmmaxkim
0
860
大規模FlutterプロジェクトのCI実行時間を約8割削減した話
teamlab
PRO
0
490
Langfuseと歩む生成AI活用推進
licux
3
290
Understanding Ruby Grammar Through Conflicts
yui_knk
1
120
TDD 実践ミニトーク
contour_gara
0
130
画像コンペでのベースラインモデルの育て方
tattaka
3
1.8k
フロントエンドのmonorepo化と責務分離のリアーキテクト
kajitack
2
130
Featured
See All Featured
Navigating Team Friction
lara
188
15k
Music & Morning Musume
bryan
46
6.7k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
9
770
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
358
30k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
283
13k
VelocityConf: Rendering Performance Case Studies
addyosmani
332
24k
Six Lessons from altMBA
skipperchong
28
4k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3k
Making the Leap to Tech Lead
cromwellryan
134
9.5k
How to train your dragon (web standard)
notwaldorf
96
6.2k
Raft: Consensus for Rubyists
vanstee
140
7.1k
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
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