添加聊天节点。js到现有的php网站。

[英]Adding chat with node.js to existing php website


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.

我有一个网站运行在共享服务器上apache/mysql/php。站点的每个页面都属于一个注册用户,这个用户一旦登录就可以编辑页面。其他非页面所有者的用户只能查看这些页面,但不能做其他任何事情。

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.

现在,在步骤3和4中,我需要知道注册用户是否仍然登录。如果是这样,那么可以将消息进一步传递给目标用户。如果没有,那么我需要发送一个消息,表明所有者(注册用户)不再可用来聊天。

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.

    使用老式的php和ajax调用。所以每个用户每一秒钟都会向服务器发送ajax请求,服务器会以某种方式跟踪每个会话。我认为相对容易实现。我并不期望会有大量的用户,但是我可以想象这对服务器来说会很沉重。

  2. using node.js.

    使用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?

    上面的解决方案1可能存在什么问题?这对不断向服务器抛出ajax请求的服务器来说是不是太沉重了?我能接受的用户数量是否合理?

  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.

    在我的共享主机上使用node js。假设可以安装它并在单独的端口上运行它,那么我应该如何检查注册用户是否仍然登录?任何想法都将非常感谢,因为我在这里没有想法。

1 个解决方案

#1


4  

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.

您是对的,PHP/Ajax调用可以导致相当大的服务器负载,特别是当您的Apache/PHP堆栈需要大量内存才能引导时。PHP系统中的许多聊天模块,例如Drupal,实际上将此责任卸载到一个专门的节点上。js服务器(您提到的第二种方法)有助于扩展。

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)来促进用户到用户的数据传输。PubNub有一个名为Presence的工具包,它可以帮助您识别每个频道的订阅者或未订阅者。

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:

为了满足您的要求,我假设每个用户在登录时都会注册到他们正在查看的页面,通过在您的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:

Presence也有一系列漂亮的特性,比如能够让所有用户订阅当前通道:

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.

我希望这能帮助您构建最小可行的产品。因为一切都是在编程语言级别实现的,所以您应该有很大的灵活性来设计一个可定制的聊天解决方案,而不会增加服务器上的复杂性或开销。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2014/03/19/4e1ee0787e041e4f2d29068175d5d9eb.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告