ขึ้นอยู่กับประเภทของแอปที่คุณกำลังสร้าง คุณอาจพบว่ามีประโยชน์ในการตรวจจับว่าผู้ใช้หรืออุปกรณ์ใดของคุณกำลังออนไลน์อยู่ — หรือที่เรียกว่าการตรวจหา "การมีอยู่" ทั้งนี้ขึ้นอยู่กับประเภทของแอปที่คุณกำลังสร้าง
ตัวอย่างเช่น หากคุณกำลังสร้างแอป เช่น โซเชียลเน็ตเวิร์ก หรือใช้งานอุปกรณ์ IoT จำนวนมาก คุณสามารถใช้ข้อมูลนี้เพื่อแสดงรายชื่อเพื่อนที่ออนไลน์และสนทนาได้ฟรี หรือจัดเรียงอุปกรณ์ IoT ของคุณตาม "เห็นล่าสุด "
Cloud Firestore ไม่รองรับการแสดงตนโดยกำเนิด แต่คุณใช้ประโยชน์จากผลิตภัณฑ์ Firebase อื่นๆ เพื่อสร้างระบบการแสดงตนได้
โซลูชัน: ฟังก์ชันคลาวด์พร้อมฐานข้อมูลเรียลไทม์
หากต้องการเชื่อมต่อ Cloud Firestore กับฟีเจอร์การแสดงตนดั้งเดิมของ Firebase Realtime Database ให้ใช้ Cloud Functions
ใช้ฐานข้อมูลเรียลไทม์เพื่อรายงานสถานะการเชื่อมต่อ จากนั้นใช้ Cloud Functions เพื่อมิเรอร์ข้อมูลนั้นไปยัง Cloud Firestore
การใช้การแสดงตนในฐานข้อมูลเรียลไทม์
ขั้นแรก ให้พิจารณาว่าระบบการแสดงตนแบบดั้งเดิมทำงานในฐานข้อมูลเรียลไทม์อย่างไร
เว็บ
// Fetch the current user's ID from Firebase Authentication. var uid = firebase.auth().currentUser.uid; // Create a reference to this user's specific status node. // This is where we will store data about being online/offline. var userStatusDatabaseRef = firebase.database().ref('/status/' + uid); // We'll create two constants which we will write to // the Realtime database when this device is offline // or online. var isOfflineForDatabase = { state: 'offline', last_changed: firebase.database.ServerValue.TIMESTAMP, }; var isOnlineForDatabase = { state: 'online', last_changed: firebase.database.ServerValue.TIMESTAMP, }; // Create a reference to the special '.info/connected' path in // Realtime Database. This path returns `true` when connected // and `false` when disconnected. firebase.database().ref('.info/connected').on('value', function(snapshot) { // If we're not currently connected, don't do anything. if (snapshot.val() == false) { return; }; // If we are currently connected, then use the 'onDisconnect()' // method to add a set which will only trigger once this // client has disconnected by closing the app, // losing internet, or any other means. userStatusDatabaseRef.onDisconnect().set(isOfflineForDatabase).then(function() { // The promise returned from .onDisconnect().set() will // resolve as soon as the server acknowledges the onDisconnect() // request, NOT once we've actually disconnected: // https://firebase.google.com/docs/reference/js/firebase.database.OnDisconnect // We can now safely set ourselves as 'online' knowing that the // server will mark us as offline once we lose connection. userStatusDatabaseRef.set(isOnlineForDatabase); }); });
ตัวอย่างนี้เป็นระบบการแสดงตนของฐานข้อมูลเรียลไทม์ที่สมบูรณ์ มันจัดการกับการตัดการเชื่อมต่อ การหยุดทำงาน และอื่นๆ หลายครั้ง
กำลังเชื่อมต่อกับ Cloud Firestore
หากต้องการใช้โซลูชันที่คล้ายกันใน Cloud Firestore ให้ใช้โค้ด Realtime Database เดียวกัน จากนั้นใช้ Cloud Functions เพื่อซิงค์ฐานข้อมูล Realtime และ Cloud Firestore
หากคุณยังไม่ได้ดำเนินการ ให้เพิ่ม Realtime Database ในโครงการของคุณและรวมโซลูชันการแสดงตนด้านบน
ถัดไป คุณจะต้องซิงโครไนซ์สถานะการแสดงตนกับ Cloud Firestore ด้วยวิธีการต่อไปนี้:
- ในเครื่องไปยังแคช Cloud Firestore ของอุปกรณ์ออฟไลน์ เพื่อให้แอปรู้ว่าออฟไลน์อยู่
- ทั่วโลก โดยใช้ฟังก์ชันคลาวด์เพื่อให้อุปกรณ์อื่นๆ ทั้งหมดที่เข้าถึง Cloud Firestore รู้ว่าอุปกรณ์เฉพาะนี้ออฟไลน์อยู่
กำลังอัปเดตแคชในเครื่องของ Cloud Firestore
มาดูการเปลี่ยนแปลงที่จำเป็นในการดำเนินการฉบับแรก - การอัปเดตแคชในเครื่องของ Cloud Firestore
เว็บ
// ... var userStatusFirestoreRef = firebase.firestore().doc('/status/' + uid); // Firestore uses a different server timestamp value, so we'll // create two more constants for Firestore state. var isOfflineForFirestore = { state: 'offline', last_changed: firebase.firestore.FieldValue.serverTimestamp(), }; var isOnlineForFirestore = { state: 'online', last_changed: firebase.firestore.FieldValue.serverTimestamp(), }; firebase.database().ref('.info/connected').on('value', function(snapshot) { if (snapshot.val() == false) { // Instead of simply returning, we'll also set Firestore's state // to 'offline'. This ensures that our Firestore cache is aware // of the switch to 'offline.' userStatusFirestoreRef.set(isOfflineForFirestore); return; }; userStatusDatabaseRef.onDisconnect().set(isOfflineForDatabase).then(function() { userStatusDatabaseRef.set(isOnlineForDatabase); // We'll also add Firestore set here for when we come online. userStatusFirestoreRef.set(isOnlineForFirestore); }); });
ด้วยการเปลี่ยนแปลงเหล่านี้ เราได้ทำให้มั่นใจได้ว่าสถานะ Cloud Firestore ในเครื่อง จะแสดงสถานะออนไลน์/ออฟไลน์ของอุปกรณ์เสมอ ซึ่งหมายความว่าคุณสามารถฟังเอกสาร /status/{uid}
และใช้ข้อมูลเพื่อเปลี่ยน UI เพื่อแสดงสถานะการเชื่อมต่อ
เว็บ
userStatusFirestoreRef.onSnapshot(function(doc) { var isOnline = doc.data().state == 'online'; // ... use isOnline });
กำลังอัปเดต Cloud Firestore ทั่วโลก
แม้ว่าแอปพลิเคชันของเราจะรายงานสถานะออนไลน์ของตัวเองอย่างถูกต้อง แต่สถานะนี้จะไม่ถูกต้องในแอป Cloud Firestore อื่นๆ เนื่องจากการเขียนสถานะ "ออฟไลน์" ของเราเป็นแบบโลคัลเท่านั้น และจะไม่ถูกซิงค์เมื่อการเชื่อมต่อกลับคืนมา เพื่อตอบโต้ เราจะใช้ Cloud Function ซึ่งเฝ้าดู status/{uid}
เส้นทางใน Realtime Database เมื่อค่า Realtime Database เปลี่ยน ค่าจะซิงค์กับ Cloud Firestore เพื่อให้สถานะของผู้ใช้ทั้งหมดถูกต้อง
โหนด js
const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(); // Since this code will be running in the Cloud Functions environment // we call initialize Firestore without any arguments because it // detects authentication from the environment. const firestore = admin.firestore(); // Create a new function which is triggered on changes to /status/{uid} // Note: This is a Realtime Database trigger, *not* Firestore. exports.onUserStatusChanged = functions.database.ref('/status/{uid}').onUpdate( async (change, context) => { // Get the data written to Realtime Database const eventStatus = change.after.val(); // Then use other event data to create a reference to the // corresponding Firestore document. const userStatusFirestoreRef = firestore.doc(`status/${context.params.uid}`); // It is likely that the Realtime Database change that triggered // this event has already been overwritten by a fast change in // online / offline status, so we'll re-read the current data // and compare the timestamps. const statusSnapshot = await change.after.ref.once('value'); const status = statusSnapshot.val(); functions.logger.log(status, eventStatus); // If the current timestamp for this data is newer than // the data that triggered this event, we exit this function. if (status.last_changed > eventStatus.last_changed) { return null; } // Otherwise, we convert the last_changed field to a Date eventStatus.last_changed = new Date(eventStatus.last_changed); // ... and write it to Firestore. return userStatusFirestoreRef.set(eventStatus); });
เมื่อคุณปรับใช้ฟังก์ชันนี้ คุณจะมีระบบการแสดงตนที่สมบูรณ์ซึ่งทำงานด้วย Cloud Firestore ด้านล่างนี้คือตัวอย่างการตรวจสอบผู้ใช้ที่ออนไลน์หรือออฟไลน์โดยใช้คำสั่ง where()
เว็บ
firebase.firestore().collection('status') .where('state', '==', 'online') .onSnapshot(function(snapshot) { snapshot.docChanges().forEach(function(change) { if (change.type === 'added') { var msg = 'User ' + change.doc.id + ' is online.'; console.log(msg); // ... } if (change.type === 'removed') { var msg = 'User ' + change.doc.id + ' is offline.'; console.log(msg); // ... } }); });
ข้อจำกัด
การใช้ฐานข้อมูลเรียลไทม์เพื่อเพิ่มสถานะในแอป Cloud Firestore ปรับขนาดได้และมีประสิทธิภาพ แต่มีข้อจำกัดบางประการ:
- Debouncing - เมื่อรับฟังการเปลี่ยนแปลงตามเวลาจริงใน Cloud Firestore โซลูชันนี้มีแนวโน้มที่จะทริกเกอร์การเปลี่ยนแปลงหลายรายการ หากการเปลี่ยนแปลงเหล่านี้ทริกเกอร์เหตุการณ์มากกว่าที่คุณต้องการ ให้ดีบักเหตุการณ์ Cloud Firestore ด้วยตนเอง
- การเชื่อมต่อ - การใช้งานนี้วัดการเชื่อมต่อกับฐานข้อมูลเรียลไทม์ ไม่ใช่ Cloud Firestore หากสถานะการเชื่อมต่อกับแต่ละฐานข้อมูลไม่เหมือนกัน โซลูชันนี้อาจรายงานสถานะการแสดงตนที่ไม่ถูกต้อง
- Android - บน Android ฐานข้อมูลเรียลไทม์จะตัดการเชื่อมต่อจากแบ็กเอนด์หลังจากไม่มีการใช้งานเป็นเวลา 60 วินาที การไม่ใช้งานหมายความว่าไม่มีผู้ฟังที่เปิดอยู่หรือการดำเนินการที่ค้างอยู่ เพื่อให้การเชื่อมต่อเปิดอยู่ เราขอแนะนำให้คุณเพิ่มตัวฟังเหตุการณ์ที่มีคุณค่าในพาธนอกเหนือจาก
.info/connected
ตัวอย่างเช่น คุณสามารถทำFirebaseDatabase.getInstance().getReference((new Date()).toString()).keepSynced()
ที่จุดเริ่มต้นของแต่ละเซสชัน สำหรับข้อมูลเพิ่มเติม โปรดดูที่ การตรวจจับสถานะการเชื่อมต่อ