dsegw3424 2014-03-19 20:13
浏览 51

将node.js的聊天添加到现有的php网站

I have a website running on shared server with apache/mysql/php. Each of the pages of the site belongs to one registered user and this user,once logged in can edit the page. Other users that are not owners of the page can only look at these pages but cannot do anything else.

Now I'm planning to add chat functionality to my application. The basic idea is that if owner of the page opens it in a browser, and is logged in, he will be shown for other users (that will be anonymous) as "available for chat". Other users visiting the page will be able to send him messages and vice versa. Anonymous users do not need to communicate with each other and they can communicate only with registered user (owner of the page). So basic structure would be like that:

  1. anonymous user(s) visits the page. Registered owner of the page is not looking at the same page and chat is not available.

  2. Registered owner logs in and opens his page in a browser. All registered users in real time are informed owner is available for chat now.

  3. Anonymous users can send him messages.

  4. Registered user receives the messages and can respond back to each user

Other users can join and chat with registered user any time. It all happens in real time and registered user can see who comes in to visit his page and goes away.

Now, in step 3 and 4 I need to know if the registered user is still logged in. If so then the messages can be passed further to intended user. If not then instead I need to send a message that the owner (registered user) is no longer available for chat.

I'm looking for advice on how to best implement it:

  1. using old school php and ajax calls. So every user would send ajax request every second or so to server and server would keep track of each conversation somehow. Relatively easy to implement I think. I'm not expecting large number of users but I can imagine this would be heavy on the server.

  2. using node.js.

Now my questions:

  1. What could be possible problem with solution 1 above. Would that be too heavy on a server constantly throwing ajax requests at it? would would be reasonable number of users I could accept?

  2. Using node js on my shared hosting.. assuming its possible to install it and run it on separate port, how would I best go about checking if registered user is still logged in or not? Any ideas would be much appreciated as am out of ideas here.

  • 写回答

1条回答 默认 最新

  • doujianzi8521 2014-03-20 03:27
    关注

    You're right that the PHP/Ajax calls can cause quite a bit of server load, especially if your Apache/PHP stack needs a lot of memory to bootstrap. Many chat modules in PHP systems, e.g. Drupal, actually offload this responsibility onto a specialized node.js server (the second approach you mentioned) to facilitate scaling.

    Another approach you may consider is to use a real-time network such as PubNub to facilitate this user-to-user data transfer. PubNub has a toolkit called Presence which can help with telling who is subscribed or unsubscribed to each channel.

    PubNub Presence demo

    To fit this to your requirements, I imagine that each user will register with the page they are viewing upon landing on it, by issuing this call in your JavaScript:

    <script src="https://cdn.pubnub.com/pubnub.min.js"></script>
    <script>
    
        var pubnub = PUBNUB({ 
            uuid : '12345-page35' //You can define this for each user
        })
    
        pubnub.subscribe({
             channel : 'site-wide-chat,page35', //Subscribe to two channels!
             message : receive_chat,            //Callback function
            presence : user_joined              //Callback function
        })
    </script>
    

    When the "owner" logs in the other users viewing the page are notified. You can accomplish that like this:

        function user_joined(event) {
            if (event.uuid.match(/page35/)) { //You can set your own test here
                // .... admin available for chat
            }
        }
    

    Presence also has a bunch of nifty features such as the ability to get all users subscribed to the current channel:

    pubnub.here_now({
        channel : 'page35',
        callback : function(m){console.log(m)}
    });
    

    I hope this helps you build your minimum viable product. Since everything is implemented at the programming language level, you should have a lot of flexibility crafting a customizable chat solution without adding additional complexity or overhead on your server.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分