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.6k
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.5k
Elixir/PhoenixによるWeb開発の現場から
ohr486
1
320
Hacking Phoenix Performance
ohr486
1
260
Plug & WAF
ohr486
2
410
elixirをプロダクションに導入する
ohr486
1
550
IEx maniacs
ohr486
4
520
Hack and Read Elixir
ohr486
2
630
Running App on AppRunner
ohr486
0
650
sponsor-talk-drecom-heisei-ruby-kaigi
ohr486
0
740
Other Decks in Programming
See All in Programming
Modern Angular with the NGRX Signal Store New Rules for Your Architecture @BASTA! 2024 in Mainz
manfredsteyer
PRO
0
280
DjangoNinjaで高速なAPI開発を実現する
masaya00
0
200
o1モデルのプロンプトエンジニアリングって?
ktc_wada
0
400
"型"のあるRailsアプリケーション開発 / Typed Rails application development
sinsoku
8
2.1k
AndroidアプリのUIバリエーションをあの手この手で確認する / Check UI variations of Android apps by various means
tkmnzm
1
470
Android開発以外のAndroid開発経験の活かしどころ
konifar
2
1.3k
The Shape of a Service Object
inem
0
600
What is TDD?
urakawa_jinsei
0
150
Pythonによるイベントソーシングへの挑戦と現状に対する考察 / Challenging Event Sourcing with Python and Reflections on the Current State
nrslib
2
410
Findy - エンジニア向け会社紹介 / Findy Letter for Engineers
findyinc
4
92k
Infrastructure as Code でセキュリティを楽にしよう!
konokenj
4
1.1k
Prolog入門
qnighy
4
1.1k
Featured
See All Featured
Music & Morning Musume
bryan
46
6k
Build your cross-platform service in a week with App Engine
jlugia
228
18k
Product Roadmaps are Hard
iamctodd
PRO
48
10k
GraphQLの誤解/rethinking-graphql
sonatard
65
9.8k
The Language of Interfaces
destraynor
154
24k
Being A Developer After 40
akosma
84
590k
Ruby is Unlike a Banana
tanoku
96
11k
Web Components: a chance to create the future
zenorocha
309
42k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
43
2k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
227
52k
Pencils Down: Stop Designing & Start Developing
hursman
119
11k
The Cult of Friendly URLs
andyhume
76
6k
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