關閉Twilio 視訊聊天室的同步問題 / Synchronous issue when closing Twilio video call room

Phanix
·
·
IPFS
·

跟同事C一起弄Twilio 網頁端視訊聊天室,先前都很正常,最近卻開始出現了奇妙的狀況。

同事C處理網頁部分client side 的 javascript 部分,我這邊是做後端的程式,要留下一些Twilio 那邊關於該session的資料。
client side 有個邏輯是,當聊天室的發起者結束聊天(或者異常斷線)的時候,會送兩個 api call。
  1. 離開聊天室的 api call 給 Twilio 伺服器
  2. 通知我們公司自己的 api 該做資料統計

其中步驟 2) 主要是要拿參與者的資料

$room = $twilio->video->v1->rooms($roomName)->fetch();

$participants = $twilio->video->rooms($roomName)->participants->read();

這兩個 api call 都是 asyn,一直以來都是運作良好。但最近發現公司 api 收到 request 後,要去 Twilio 拿聊天室資訊時,會發現無法拿到聊天室資訊。

仔細查了之後才發現,以前 1) js 送給 Twilio 的request 都比較慢才送到,而 2) js 送給我們自己 server ,讓api程式去跟 Twilio 拿聊天室資訊的 request 反而很快就拿到。

而 Twilio 的規則是當最後一個聊天室的參加者離開之後,聊天室就會被關閉,因此當最近 1) 的 request 比較快送到時,如果其他聊天室參與者都已經先行離開,那麼聊天室會被關閉,步驟 2) 就拿不到狀態是使用中(in-progress)的聊天室資料,必須要另外指定參數才能拿到聊天室資訊。

$rooms = $twilio->video->v1->rooms ->read(["status" => "completed", "uniqueName" => "DailyStandup"], 20 );

結論就是,把聊天室發起者離開的流程改成只發一個 api call 到我們自己的伺服器,然後由我們自己去發送關閉聊天室 api call 給 Twilio,這樣就可以避免 async api call 可能會有先後次序的問題。

My colleague takes the client side javascript, and I handle the server side api to retrieve some video call session data from Twilio.
On client side, we will send out two api calls asynchronously when the video call originator want to close the call room (or connection failed when we detected).
  1. leave (disconnect) video call room request to Twilio
  2. notify api on our server to collecting session data

Step 2) includes some data collection, such as the participant information.

$room = $twilio->video->v1->rooms($roomName)->fetch();

$participants = $twilio->video->rooms($roomName)->participants->read();

These two apis coworked well until now, and we recently found that step 2) receives “room not found” error message from Twilio, not the room information as expected.

After a few hour investigation, we found some interesting (weird, too) situation that Twilio received step 1) api call later than the api call we attempted to get room information in step 2) in the past. (Maybe the internet connection from our office to Twilio is better than before?)

And Twilio video call room service will close the room if the last participant leaves. So, if the room originator is the last live person, and Twilio receives the api call in step 1), the call room will be closed (Twilio uses “completed” as the status term). If we want to get the completed room information, we cannot use the preceding code segment (can only get “in-progress” room information), and should specify the arguments as below instead.

$rooms = $twilio->video->v1->rooms ->read(["status" => "completed", "uniqueName" => "DailyStandup"], 20 );

Eventually, we change the logic when the room originator leaves room. Skip step 1), and let step 2) calls Twilio close room api after we collect the data we needed.

Original link: Phanix's Blog

CC BY-NC-ND 2.0 授权

喜欢我的作品吗?别忘了给予支持与赞赏,让我知道在创作的路上有你陪伴,一起延续这份热忱!