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
420
Hacking Phoenix Performance
ohr486
1
300
Plug & WAF
ohr486
2
460
elixirをプロダクションに導入する
ohr486
1
600
IEx maniacs
ohr486
4
560
Hack and Read Elixir
ohr486
2
680
Running App on AppRunner
ohr486
0
720
sponsor-talk-drecom-heisei-ruby-kaigi
ohr486
0
800
Other Decks in Programming
See All in Programming
定理証明プラットフォーム lapisla.net
abap34
1
1.8k
Pythonでもちょっとリッチな見た目のアプリを設計してみる
ueponx
1
570
クリーンアーキテクチャから見る依存の向きの大切さ
shimabox
2
490
PHP ステートレス VS ステートフル 状態管理と並行性 / php-stateless-stateful
ytake
0
100
PHPカンファレンス名古屋2025 タスク分解の試行錯誤〜レビュー負荷を下げるために〜
soichi
1
220
CI改善もDatadogとともに
taumu
0
120
『GO』アプリ バックエンドサーバのコスト削減
mot_techtalk
0
150
データの整合性を保つ非同期処理アーキテクチャパターン / Async Architecture Patterns
mokuo
49
17k
なぜイベント駆動が必要なのか - CQRS/ESで解く複雑系システムの課題 -
j5ik2o
12
4.1k
Introduction to kotlinx.rpc
arawn
0
700
Honoのおもしろいミドルウェアをみてみよう
yusukebe
1
210
『GO』アプリ データ基盤のログ収集システムコスト削減
mot_techtalk
0
130
Featured
See All Featured
A Tale of Four Properties
chriscoyier
158
23k
Into the Great Unknown - MozCon
thekraken
35
1.6k
Optimizing for Happiness
mojombo
376
70k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
12
960
For a Future-Friendly Web
brad_frost
176
9.5k
Designing Experiences People Love
moore
140
23k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
4
410
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
27
1.9k
Bash Introduction
62gerente
611
210k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7.1k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
45
9.4k
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