如何将与nodejs和xmpp的聊天集成到现有的web应用程序中?

时间:2022-05-01 15:51:40

I have read a lot of questions relating this, but none of them are satisfying.

我读过许多有关这方面的问题,但没有一个是令人满意的。

Existing App

现有的应用程序

A minimalist social network implemented using Expressjs as an API.Using MySql as DB.socket.io for notifications and ember.js as a frontend framework.

使用Expressjs作为API实现的极简社交网络。使用MySql作为DB.socket。通知和余烬的io。js作为前端框架。

What I want to integrate

我想要整合的东西!

I want to implements only a few features of XMPP such as

我只想实现XMPP的一些特性,比如

  • Peer 2 Peer Messaging
  • 点2点消息
  • Presence and Last Seen
  • 存在和最后一次看到
  • Group Chat
  • 群组聊天
  • Read Receipts
  • 阅读收据

A basic idea I got from reading similar questions.

我从类似的问题中得到了一个基本的想法。

  1. Need a client library (Strophe.js,Converse.js)
  2. 需要一个客户端库(Strophe.js,Converse.js)
  3. Need a XMPP server (ejabberd,Openfire,Prosody)
  4. 需要一个XMPP服务器(ejabberd,Openfire,Prosody)

Questions

问题

  1. How do I integrate chat here ?
  2. 如何在这里集成聊天?
  3. How do I authenticate XMPP users (FYI, I have JWT Authentication implemented currently) ?
  4. 如何对XMPP用户进行身份验证?
  5. Suggestions on using redis(pub/sub) with socket.io or mqtt pub/sub for implementing the chat.Is it scalable ? / What about performance ?
  6. 使用redis(pub/sub)和socket的建议。io或mqtt pub/sub实现聊天。它是可伸缩的吗?/演出怎么样?

What I asked might be too broad.But still don't have any idea on using which set technologies to use.

我的要求可能太宽泛了。但是仍然不知道使用哪种技术。

3 个解决方案

#1


1  

For learning purpose you can achieve all things using ejabberd+converse.js Below steps will setup environment in ubuntu

为了学习目的,你可以使用ejabberd+converse来实现所有的事情。下面的步骤将在ubuntu中设置环境

  1. setup ejabberd by following https://www.digitalocean.com/community/tutorials/how-to-install-ejabberd-xmpp-server-on-ubuntu
  2. 通过遵循https://www.digitalocean.com/community/tutorials/how- install-ejabberd-xmpp-server-on-ubuntu来设置ejabberd
  3. create a host binding by editing /etc/hosts file in ubuntu

    通过在ubuntu中编辑/etc/hosts文件创建主机绑定

    127.0.1.2       talk.rajesh6115.local
    
  4. install apache2 using

    输入安装使用

    sudo apt-get update
    sudo apt-get install apache2
    
  5. setup a virtual host for bosh (XEP-0206) in your apache like below /etc/apache2/sites-available/talk.rajesh6115.local.conf

    在apache中为bosh (XEP-0206)设置一个虚拟主机,如下/etc/apache2/sites-available/talk.rajesh6115.local.conf

    <VirtualHost *:80>
        ServerName talk.rajesh6115.local
        ServerAlias www.talk.rajesh6115.local
        ServerAdmin webmaster@talk.rajesh6115.local
        DocumentRoot /var/www/talk.rajesh6115.local
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        ProxyPass /http-bind http://talk.rajesh6115.local:5280/http-bind/
        ProxyPassReverse /http-bind http://talk.rajesh6115.local:5280/http-bind/
    </VirtualHost>
    
  6. now you can configure converse js to point to your bosh service, then your communications starts

    现在可以配置converse js来指向bosh服务,然后开始通信

NOTE:

注意:

setup a virtual host in ejabberd by adding a line like below

通过添加如下所示的行,在ejabberd中设置一个虚拟主机

    hosts:
      - "talk.rajesh6115.local"

setup a admin login. using this login you can create user.

设置一个管理员登录。使用这个登录可以创建用户。

  admin:
     user:
         - "admin": "talk.rajesh6115.local"
  1. for make conversejs talk with xmpp server only one thing you have to give that is bosh serivce url. for more details https://conversejs.org/docs/html/development.html#initialize
  2. 对于让conversejs与xmpp服务器对话,您只需提供一个东西,那就是bosh serivce url。更多细节https://conversejs.org/docs/html/development.html初始化

7.finally how to integrate with web application?

7所示。最后,如何与web应用程序集成?

Method1 (simple): use same logins for webapp and xmpp means rajesh@talk.rajesh6115.local can be a email address also a valid jid

Method1(简单):在webapp中使用相同的登录,xmpp意味着rajesh@talk.rajesh6115。local可以是一个电子邮件地址,也可以是一个有效的jid

Method2: Use a authentication server which will return both jid and password after successful authentication, then start your xmpp session using provided credential.

Method2:使用一个身份验证服务器,它将在成功的身份验证之后返回jid和密码,然后使用提供的凭据启动xmpp会话。

#2


0  

I suggest to use socket.io, since you are currently using JWT authentication and it can be implementing authentication using NodeJS. You can create so call room in socket.io for peer to peer to messaging or goroup chat. I read your comment and since it's for learning purposes Mysql is scalable enough. For the sake of performance using a load balancer like nginx or even can use NodeJS load balancer with horizontally scaling technique can extend performance easily. Hope it help.

我建议用插座。io,因为您目前正在使用JWT身份验证,它可以使用NodeJS实现身份验证。您可以在socket中创建so call room。io用于对等点对消息或goroup聊天。我读了你的评论,因为这是为了学习,Mysql是可扩展的。为了提高性能,可以使用像nginx这样的负载均衡器,甚至可以使用NodeJS负载均衡器,使用水平伸缩技术可以轻松扩展性能。希望它帮助。

#3


0  

Given your original idea how to solve this, I'd suggest that you take a look at node-xmpp-client and node-xmpp-server.

考虑到您最初的解决方法,我建议您看看node-xmpp-客户端和node-xmpp-服务器。

It's an excellent set of libraries and you can use them to fully integrate your application on a nodejs level. So you'd be able to control authentication yourself (use existing users/pws in your app?), and be notified when a message in a (group) chat appears.

这是一组优秀的库,您可以使用它们在nodejs级别上全面集成应用程序。因此,您可以自己控制身份验证(使用应用程序中的现有用户/pws ?),并在(组)聊天出现消息时得到通知。

Of course you can use an existing server like prosody or ejabberd, as a backend for chats. In my experience it's not much work to get node-xmpp-client integrated. But building/running an XMPP-server with nodejs (that can actually talk to other servers) is not that trivial - a little more that the examples thrown together yield, unfortunately.

当然,您可以使用现有的服务器(如prosody或ejabberd)作为聊天的后端。在我的经验中,集成node-xmpp-client的工作并不多。但是用nodejs构建/运行一个xmpp -服务器(它实际上可以与其他服务器通信)并不是那么简单——不幸的是,这些例子加在一起会产生一些收益。

Also, XMPP being text-based, actually even worse, xml-based... it's not really the definition of efficient. Let alone the complexity of all the modules supporting node-xmpp :)

而且,XMPP是基于文本的,更糟的是,基于xml……这并不是效率的定义。更不用说支持node-xmpp的所有模块的复杂性了:)

So

所以

If you are worried about performance and don't need XMPP per-se and really only want the features above, XMPP is a bad choice. It's far to wasteful for your original purposes.

如果您担心性能,并且不需要XMPP本身,并且只需要上面的特性,那么XMPP是一个糟糕的选择。这对你最初的目的来说是很浪费的。

So something like zmq should enable you to implement group and personal chats.

因此,像zmq这样的东西应该使您能够实现组和个人聊天。

redis could be used to save chat histories, presence information and message reciepts.

redis可以用来保存聊天记录、显示信息和消息接收。

To my knowledge there is no library for node that would just give you what you want for free, and IMHO the way using XMPP is even harder than implementing your features with tools like zmq and a datastore as backing on your own.

据我所知,没有一个用于node的库可以免费提供您想要的东西,而且使用XMPP的方式甚至比使用zmq和datastore等工具实现您的特性更困难。

#1


1  

For learning purpose you can achieve all things using ejabberd+converse.js Below steps will setup environment in ubuntu

为了学习目的,你可以使用ejabberd+converse来实现所有的事情。下面的步骤将在ubuntu中设置环境

  1. setup ejabberd by following https://www.digitalocean.com/community/tutorials/how-to-install-ejabberd-xmpp-server-on-ubuntu
  2. 通过遵循https://www.digitalocean.com/community/tutorials/how- install-ejabberd-xmpp-server-on-ubuntu来设置ejabberd
  3. create a host binding by editing /etc/hosts file in ubuntu

    通过在ubuntu中编辑/etc/hosts文件创建主机绑定

    127.0.1.2       talk.rajesh6115.local
    
  4. install apache2 using

    输入安装使用

    sudo apt-get update
    sudo apt-get install apache2
    
  5. setup a virtual host for bosh (XEP-0206) in your apache like below /etc/apache2/sites-available/talk.rajesh6115.local.conf

    在apache中为bosh (XEP-0206)设置一个虚拟主机,如下/etc/apache2/sites-available/talk.rajesh6115.local.conf

    <VirtualHost *:80>
        ServerName talk.rajesh6115.local
        ServerAlias www.talk.rajesh6115.local
        ServerAdmin webmaster@talk.rajesh6115.local
        DocumentRoot /var/www/talk.rajesh6115.local
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        ProxyPass /http-bind http://talk.rajesh6115.local:5280/http-bind/
        ProxyPassReverse /http-bind http://talk.rajesh6115.local:5280/http-bind/
    </VirtualHost>
    
  6. now you can configure converse js to point to your bosh service, then your communications starts

    现在可以配置converse js来指向bosh服务,然后开始通信

NOTE:

注意:

setup a virtual host in ejabberd by adding a line like below

通过添加如下所示的行,在ejabberd中设置一个虚拟主机

    hosts:
      - "talk.rajesh6115.local"

setup a admin login. using this login you can create user.

设置一个管理员登录。使用这个登录可以创建用户。

  admin:
     user:
         - "admin": "talk.rajesh6115.local"
  1. for make conversejs talk with xmpp server only one thing you have to give that is bosh serivce url. for more details https://conversejs.org/docs/html/development.html#initialize
  2. 对于让conversejs与xmpp服务器对话,您只需提供一个东西,那就是bosh serivce url。更多细节https://conversejs.org/docs/html/development.html初始化

7.finally how to integrate with web application?

7所示。最后,如何与web应用程序集成?

Method1 (simple): use same logins for webapp and xmpp means rajesh@talk.rajesh6115.local can be a email address also a valid jid

Method1(简单):在webapp中使用相同的登录,xmpp意味着rajesh@talk.rajesh6115。local可以是一个电子邮件地址,也可以是一个有效的jid

Method2: Use a authentication server which will return both jid and password after successful authentication, then start your xmpp session using provided credential.

Method2:使用一个身份验证服务器,它将在成功的身份验证之后返回jid和密码,然后使用提供的凭据启动xmpp会话。

#2


0  

I suggest to use socket.io, since you are currently using JWT authentication and it can be implementing authentication using NodeJS. You can create so call room in socket.io for peer to peer to messaging or goroup chat. I read your comment and since it's for learning purposes Mysql is scalable enough. For the sake of performance using a load balancer like nginx or even can use NodeJS load balancer with horizontally scaling technique can extend performance easily. Hope it help.

我建议用插座。io,因为您目前正在使用JWT身份验证,它可以使用NodeJS实现身份验证。您可以在socket中创建so call room。io用于对等点对消息或goroup聊天。我读了你的评论,因为这是为了学习,Mysql是可扩展的。为了提高性能,可以使用像nginx这样的负载均衡器,甚至可以使用NodeJS负载均衡器,使用水平伸缩技术可以轻松扩展性能。希望它帮助。

#3


0  

Given your original idea how to solve this, I'd suggest that you take a look at node-xmpp-client and node-xmpp-server.

考虑到您最初的解决方法,我建议您看看node-xmpp-客户端和node-xmpp-服务器。

It's an excellent set of libraries and you can use them to fully integrate your application on a nodejs level. So you'd be able to control authentication yourself (use existing users/pws in your app?), and be notified when a message in a (group) chat appears.

这是一组优秀的库,您可以使用它们在nodejs级别上全面集成应用程序。因此,您可以自己控制身份验证(使用应用程序中的现有用户/pws ?),并在(组)聊天出现消息时得到通知。

Of course you can use an existing server like prosody or ejabberd, as a backend for chats. In my experience it's not much work to get node-xmpp-client integrated. But building/running an XMPP-server with nodejs (that can actually talk to other servers) is not that trivial - a little more that the examples thrown together yield, unfortunately.

当然,您可以使用现有的服务器(如prosody或ejabberd)作为聊天的后端。在我的经验中,集成node-xmpp-client的工作并不多。但是用nodejs构建/运行一个xmpp -服务器(它实际上可以与其他服务器通信)并不是那么简单——不幸的是,这些例子加在一起会产生一些收益。

Also, XMPP being text-based, actually even worse, xml-based... it's not really the definition of efficient. Let alone the complexity of all the modules supporting node-xmpp :)

而且,XMPP是基于文本的,更糟的是,基于xml……这并不是效率的定义。更不用说支持node-xmpp的所有模块的复杂性了:)

So

所以

If you are worried about performance and don't need XMPP per-se and really only want the features above, XMPP is a bad choice. It's far to wasteful for your original purposes.

如果您担心性能,并且不需要XMPP本身,并且只需要上面的特性,那么XMPP是一个糟糕的选择。这对你最初的目的来说是很浪费的。

So something like zmq should enable you to implement group and personal chats.

因此,像zmq这样的东西应该使您能够实现组和个人聊天。

redis could be used to save chat histories, presence information and message reciepts.

redis可以用来保存聊天记录、显示信息和消息接收。

To my knowledge there is no library for node that would just give you what you want for free, and IMHO the way using XMPP is even harder than implementing your features with tools like zmq and a datastore as backing on your own.

据我所知,没有一个用于node的库可以免费提供您想要的东西,而且使用XMPP的方式甚至比使用zmq和datastore等工具实现您的特性更困难。