2016-12-16 8 views
1

私は現在リアルタイムSMSシステムに取り組んでいますが、私の現在の実装はひどい習慣であり、私はこれをより効率的にするためのガイダンスを探しています。PHPとTwilioリアルタイム?

あなたはそれはあなたが選択したその番号のすべてのテキストメッセージを引っ張るインタフェースをロードしようとしました現在

、。それから、リストの最後のメッセージよりも新しいメッセージを求めて書いたTwilio JSON PHPスクリプトに、5秒ごとにajaxコールを発生させます。ここ

$.getJSON("/includes/twilio.php",{action:"getconvo",cid:customer.customer_number},function(data){ 
     $('#sms_messages').html("<div></div>"); 
     $(data.messages).each(function(){ 
      insertSMS(this.msg,this.date,this.from); 
      lastMessage = this.date; 
     }); 
     $("#sms_messages").animate({ scrollTop: $('#sms_messages > div').height()},"fast"); 
     shouldUpdate = true; 
     sms_interval = setInterval(function(){updateSMS(customer.customer_number)},5000); 
}); 

アップデート機能

function updateSMS(cid){ 
    if(shouldUpdate){ 
     $.getJSON("/includes/twilio.php",{action:"getconvo",cid:cid,date:lastMessage},function(data){ 
      if(data.messages.length > 0){ 
       // Play an embeded sound effect when a new message is found. 
       $('#sms_sound')[0].play(); 
       $(data.messages).each(function(){ 
        insertSMS(this.msg,this.date,this.from); 
        lastMessage = this.date; 
       }); 
       $("#sms_messages").animate({ scrollTop: $('#sms_messages > div').height()},"fast"); 
      } 
     }); 
    } 
} 
+0

コードレビューのリクエストは、ここではオフトピックです。そのような質問には適切な[SEサイト](http://codereview.stackexchange.com/)があります。 – hindmost

答えて

2

Twilioの開発者エバンジェリスト。

"リアルタイム" SMSメッセージを取得するためにTwilio APIをポーリングすることはお勧めしません。ポーリングは非効率的で、Twilioのサーバーだけでなくサーバーも、必要以上に長くビジー状態に保ちます。

ではなく、Twilioのwebhooks to receive messagesを使用します。次に、メッセージを受信するページ上に、Webhookから受信した新しいメッセージをページにプッシュするために、サーバー送信イベント(see this article for an in depth description of SSE as well as example PHP code to implement)またはWebソケット(ここではweb sockets in PHPlibrary built as part of itに関する記事)を実装します。

それがまったく役に立ったら教えてください。

+0

ありがとうございます。私は記事を掘り下げて、私が思いつくことができるものを見ていきます。 :) –

+0

恐ろしい、それがうまく行くことを願って! – philnash

関連する問題