Can I implement chat functionality via Firebase Cloud Functions?
我正在尝试通过将Angular用于前端并将Cloud Function与Cloud Firestore用作后端来实现聊天功能。前端和Firestore仅通过Cloud Functions进行通信。顺便问一下,这是一个好方法吗?
我所知道的问题是,如果我的用户所在的聊天室之一收到新消息,则可以使我的前端实时更新。
一个聊天室仅限两个用户使用,但是一个用户可以与其他用户建立多个聊天室。
我的代码:
云功能:
chatroomsEndpoint.js:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | const chatroomsCollection = db.collection("chatrooms"); exports.getAllChatroomsByUserId = async function (req, res) { try { let chatrooms = []; // console.log(req.params.user_id); const resultA = await chatroomsCollection.where("userA","==", req.params.user_id).get(); resultA.docs.map(chatroom => { return chatrooms.push(chatroom.data()); }) const resultB = await chatroomsCollection.where("userB","==", req.params.user_id).get(); resultB.docs.map(chatroom => { return chatrooms.push(chatroom.data()); }) return res.status(200).send(chatrooms); } catch (error) { console.log('Error getting chatrooms', error); return res.send(false); } }; |
我在Postman上进行了尝试,并获得了当前用户所在的所有聊天室。
前端(带有angular)
聊天服务.ts:
1 2 3 4 5 6 7 8 9 10 11 | constructor( private authService: AuthService, private http: HttpClient, private router: Router ) { this.headers.append('Content-Type', 'application/json'); } getAllChatroomsByUserId() { return this.http.get(`${this.apiUrl2}/${this.authService.getUser().uid}`); } |
chatroom-list.component.ts:
1 2 3 4 5 6 7 8 9 10 11 12 | userChats$; constructor( public authService: AuthService, public chatService: ChatService, public router: Router, public userStoreService: UserStoreService ) { } ngOnInit() { this.userChats$ = this.chatService.getAllChatroomsByUserId(); } |
chatroom-list.component.html:
1 2 3 4 5 6 7 8 9 | <ul> <li *ngFor="let chatroom of userChats$ | async"> {{ chatroom.id }} - {{ chatroom.messages.length }} Messages </li> </ul> |
在启动Web应用程序时,我得到的只是聊天中的消息数,但是如果我在Firestore中的文档中添加另一条消息,则消息的值不会更新。
我尝试坚持使用视频https://www.youtube.com/watch?v=LKAXg2drQJQ中的示例
我认为无法以您希望的方式使用云功能来实现。尽管有用于云功能的Firestore触发器,但是您很快就会遇到一个更大的问题……如何将新事件从服务器发送回浏览器。
正如您在视频中看到的,他正在浏览器上使用Firestore侦听器(如.snapshotChanges)来侦听从数据库触发的事件。之所以可以在浏览器环境中运行,是因为Firebase在浏览器和服务器之间打开了一个websocket,从而允许了双方之间的实时通信……而更深层次的是,由于Firestore内置了websocket支持,因此可以正常工作。如果您想构建一个应用程序来侦听数据库,Firestore将允许您执行此操作。
不幸的是,云功能没有相同的功能。它们并不是像Firestore的websocket那样"保持开放"。实际上,如果您尝试一下,它们将在几分钟后超时...即使您调整了超时限制,您仍然会遇到其他问题...例如,如何处理断开的连接? (Firestore网络套接字将自动尝试重新连接并重新打开该频道),哪些用户在听什么?等等...重点是,云功能的含义是简短,"执行一次",一种服务...它们不会"保持打开状态"并在15分钟内继续向客户端提供更新时间跨度。
长话短说,如果您希望浏览器从Firebase接收实时更新,则浏览器需要直接订阅那些Firestore事件-无云功能中间人-就像您在视频中看到的一样。
编辑:
有关实时数据的答案
n