2017-08-30 13 views
1

私は自分でリアルタイム通知システムを構築しようとしています。リアルタイムWeb通知サービス

最後の2日間私は多くを検索しました。私は最も良い、最も簡単な解決策はnode.jssocket.ioを使って開発することだと信じています。しかし、私はどちらも認識していません。

node.jssocket.ioはそれを実現するための良い方法ですか?

仕様DBに格納されるユーザーの

  • 二つのグループ(簡単なユーザ&管理者)
  • すると簡単なユーザポスト何か、ポストは、(すべての)管理者にsendedではれる
  • 管理者が投稿に返信すると、返信は特定のユーザーにのみ送信されます

ここでは何かを始めるための簡単な例やチュートリアルはありますか? 誰かが例を投稿できるとすれば、それは私にとって非常に役に立つでしょう。

+0

回答を参照してください – turmuka

答えて

1

私はyouse MongoDBとAjaxをお勧めします。それはずっと簡単です、あなたはクライアント側(html)にajaxコードを追加し、サーバー側でリクエストを処理するだけです。

簡単な例:

通常、ユーザーがメッセージ、彼らが受けていることを知っているため

サーバー側

app.post('/myUrl', function(req, res){ 

    if(req.body){ 
    //message handlers here 
    } 
    Users.find({type: 'admin'}, function(err, users){ 
    var message = req.body.message; 
    for(var i = 0; i < users.length, i++){ 
     //make sure you have the type as adminPending from the schema in MongoDB 
     message.save(//save this message to the database); //save this message to the database as 'adminPendingType' 
    } 
    }) 
}) 

は、管理者に来

htmlファイル

$.ajax({ 
    method: "POST", 
    url: "http://myUrl.com/myPath", 
    data: { message: "Hello this is a message" }, 
    contentType: "application/json", 
    success: function(data){ 
    //handle success 
    }, 
    error: function(err){ 
    //error handler 
    }  
}) 
を送信メッセージ、あなたは毎秒AJAXコールをする必要がありますd、これはfacebook/twitterがほとんどのものを扱う方法です。だから、基本的にサーバに何度も何度も何度も何度も尋ねる。

管理HTML

function messageGetter(){ 

    $.ajax({ 
     method: "POST", 
     url: "http://myUrl.com/didIreceiveAmessage", 
     data: { message: "Hello this is a message" }, 
     contentType: "application/json", 
     success: function(data){ 
     //success handler with data object 
     if(data['exists']== "true"){ 
      //add your data.message to the html page, so it will be seen by the user 
     } 
     }, 
     error: function(err){ 
     //error handler 
     }  
    }) 

} 

setInterval(messageGetter, 1000); //check it each second 

サーバー側

app.post('/myUrl', function(req, res){ 

    if(req.body){ 
    //message handlers here 
    } 
    Message.find({type: 'adminPending'}, function(err, messages){ 
    //find the admin info from cookies here 
    if(messages.length == 0){ 
     console.log("No messages pending"); 
     return false; //exit the request 
    }else{ 
     var admin = req.session.admin.id; //admin user 
     //handle stuff with admin 
     messages['exists'] == true; 
     res.send(messages); 
     //change the type of message from adminPending to adminSeen 
     return false; //exit the message 
    } 
    }) 
}) 

これは、ノードとAJAXとMongoDBのでそれを行う方法についてだけで素早く簡単な例でした。もちろん、メッセージのタイプを変更して保存する必要があるので、コーディングはもっと長くなります。

+0

あなたの時間とあなたのポストのために多くのことを後悔しています。私はそれを考慮する。 – GeoDim

+0

ありがとうございます@GeoDim – turmuka

関連する問題