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
RailsとFirebaseで作るリアルタイムチャット
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
moai
March 26, 2021
Programming
790
0
Share
RailsとFirebaseで作るリアルタイムチャット
Railsとfirebaseでどのようにリアルタイムチャットを作ったのかを話します。
moai
March 26, 2021
More Decks by moai
See All by moai
プロダクトビジョンから作る機能を落とし込む
ryosukeyamazaki
2
510
with コロナは支社でも成果が出しやすい
ryosukeyamazaki
1
190
自分らしいリーダーシップ
ryosukeyamazaki
2
2.7k
エンジニアが始めるUX(5分版)
ryosukeyamazaki
0
260
広く複雑なプロダクトの 高速キャッチアップに取り組む
ryosukeyamazaki
0
9.9k
エンジニアが始めるUX
ryosukeyamazaki
3
11k
Other Decks in Programming
See All in Programming
JAWS-UG横浜 #100 祝・第100回スペシャルAWS は VPC レスの時代へ
maroon1st
0
200
My daily life on Ruby
a_matsuda
2
130
Explore CoroutineScope
tomoeng11
0
140
書籍「ユーザーストーリーマッピング」が私のバイブル
asumikam
4
460
PicoRuby for IoT: Connecting to the Cloud with MQTT
yuuu
2
720
JOAI2026 1st solution - heron0519 -
heron0519
0
170
実用!Hono RPC2026
yodaka
2
290
Back to the roots of date
jinroq
0
620
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
440
10 Tips of AWS ~Gen AI on AWS~
licux
5
520
Claude CodeでETLジョブ実行テストを自動化してみた
yoshikikasama
0
1.1k
20年以上続くプロダクトでも使い続けられる静的解析ツールを求めて
matsuo_atsushi
0
120
Featured
See All Featured
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Into the Great Unknown - MozCon
thekraken
41
2.4k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
52k
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
Claude Code のすすめ
schroneko
67
220k
Art, The Web, and Tiny UX
lynnandtonic
304
21k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
130
Ethics towards AI in product and experience design
skipperchong
2
270
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
190
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.4k
sira's awesome portfolio website redesign presentation
elsirapls
0
230
Google's AI Overviews - The New Search
badams
0
990
Transcript
freee 株式会社 freee Slide Format RailsとFirebaseで作るリアルタイムチャット
• freee株式会社所属 • Engineer • 開発効率高めるのが好き • 趣味はrubocopの適用 山崎遼介 moai
Engineer 2
3 今日のゴール RailsとFirebaseで作るリアルタイムチャットの仕組みを紹介
4 前提 作りたい機能、技術stack
5 プロジェクト管理freee • プロジェクトにまつわる業務を楽にしたい
6 作るモチベーション • プロジェクトに関する情報を雑に記録する • 経営者 <-> プロジェクトマネージャー 間で気になったことを聞ける
7 •
8 技術スタック:前提 • Ruby on Rails • OpenApi • TypeScript
• React
9 課題
10 リアルタイムのチャットをどう作るか?(ざっくり) • つまりはポーリングどうするの? ◦ ロングポーリング? ◦ 世の中に有るイベントサブスクライブサービス使う? ◦ Firebase?(NEW!)
11 Firebase使う懸念は? • 個人情報はどこに置くのか? ◦ Firebaseに個人情報置く? ◦ 置くとするとセキュリティはどう担保する? • Railsとの連携はどうするのか?
◦ RailsとID連携?
12 Firebase使う懸念は? • 個人情報はどこに置くのか? ◦ Firebaseに個人情報置く? ◦ 置くとするとセキュリティはどう担保する? • Railsとの連携はどうするのか?
◦ RailsとID連携?
13 どうなったの?
14 アーキテクチャざっくり Data Fetch 個人情報のやり取り Event Publish イベントが 起こったことだけ
を通知 Event Subscribe イベントが 起きたことだけを受 信 新しい Event 有る?
15 Rails側擬似コード class TimeLineMessage < ApplicationRecord after_create_commit -> { Gcp::Firestore.client.col('timeline_notifications').doc.set(
identifier: "#{id}-timeline", created_at: Time.current ) } end
16 firebaseのデータ
17 JavaScript側擬似コード export const ChatContainer = (id) => { 2
const [timelines, setTimeLines] = React.useState([]); const [latestId, setLatestId] = React.useState(0); 3 const reloadTimeline = (id) => { Api.fetchTimeline(setTimeLines); setLatestId(id);}; 4 const usePushNotification(reloadTimeline) = firestore 5 .collection('timeline_notifications').where('identifier', '==', "${id}-timeline") 6 .limit(1).nSnapshot(querySnapshot => { 7 if( querySnapshot.docs[0].id !== latestId ) reloadTimeline(querySnapshot.docs[0].id); 8 }); 9 React.useEffect(() => { usePushNotification(reloadTimeline) } ); 10 return (<Table rows={timelines} />); }
18 良かったこと
19 サブスクライブ処理はfirebaseにおまかせできた 新しい Event 有る? ここはFirebase Client がやってくれる Data Fetch
個人情報のやり取り Event Subscribe イベントが 起きたことだけを受 信 Event Publish イベントが 起こったことだけ を通知
20 フロント側の処理はここだけ export const ChatContainer = (id) => { 2
const [timelines, setTimeLines] = React.useState([]); const [latestId, setLatestId] = React.useState(0); 3 const reloadTimeline = (id) => { Api.fetchTimeline(setTimeLines); setLatestId(id);}; 4 const usePushNotification(reloadTimeline) = firestore 5 .collection('timeline_notifications').where('identifier', '==', "${id}-timeline") 6 .limit(1).nSnapshot(querySnapshot => { 7 if( querySnapshot.docs[0].id !== latestId ) reloadTimeline(querySnapshot.docs[0].id); 8 }); 9 React.useEffect(() => { usePushNotification(reloadTimeline) } ); 10 return (<Table rows={timelines} />); }
21 Firebaseには顧客情報置かなくて済んだ class TimeLineMessage after_create_commit -> { Gcp::Firestore.client.col('timeline_notifications').doc.set( identifier: "#{id}-timeline",
created_at: Time.current ) } end 通知イベントのみFirebaseに連携
22 じつは
23 社内で先行事例が • Takumi Ohashi@_tohashi による Firebaseを使ってチャット機能を作る先行事例があった
24 まとめ
25 まとめ • FirebaseをEvent通知だけに限定して使うのはめちゃ良い • Railsと一緒に使う場合もそんなに悪くない。 ◦ ただ、ログイン連携は闇の気配。
Firebaseはいいね
27 EOF