Build an instant messaging system based on XMPP

1 References

2 Knowledge about instant messaging

2.1 Overview of instant messaging

Instant Messaging (Instant Messaging) is currently the most popular communication method on the Internet, and various instant messaging software emerges in endlessly; service providers also provide more and more abundant communication service functions. Common instant messaging tools include QQ, WeChat etc. However, if you want to build an instant messaging system, you cannot do without the instant messaging protocol. Today I will introduce an instant messaging system based on the XMPP protocol.

The Instant Messaging protocol is the specification that the instant messaging system needs to follow when communicating, and it defines some standardized communication data formats and network architectures.

Common Protocols for Instant Messaging

  1. Instant Messaging and Spatial Protocol (IMPP)
  2. Spatial and Instant Messaging Protocol (PRIM)
  3. Initiation Protocol (SIP) for instant messaging and space balancing extensions
  4. Extensible Messaging and Presentation Protocol (XMPP)
  5. Private protocol (custom protocol): In order to better meet specific needs and security, some developers will redefine a set of protocols. This cost is very huge, and it is usually done by large manufacturers (such as Tencent).

2.2 Knowledge about XMPP

2.2.1 Definition of XMPP

XMPP (Extensible Messaging and Presentation Protocol) is a protocol based on Extensible Markup Language (XML), which inherits the flexible development in the XML environment and can be used for real-time messaging, presentation and request/response services. XMPP uses TCP to transmit XML data streams, which is an application layer protocol rather than a transport layer protocol. XMPP official website    XMPP Chinese

The predecessor of the XMPP protocol is Jabberthat adopting the XMPP protocol to implement IM mainly considers that the XMPP protocol is based on XML. This shows that XMPP is extensible, so XMPP information can not only be simple text, but also can carry complex data and files in various formats, which means that XMPP protocol can not only be used in communication between people, but also Realize the communication between software and software or between software and people. Currently, instant messaging tools that support XMPP protocol include Gtalk, FaceBook IM, Twitter, Netease POPO and other communication tools, which have a very good development prospect.

Information: XMPP for the selection of instant messaging protocols

2.2.2 Basic network structure of XMPP

There are three roles defined in XMPP, client, server, and gateway.

  • Server: It also undertakes the functions of client information recording, connection management and information routing.
  • Gateway: A special-purpose server-side service whose main function is to translate xmpp into an external message system and translate returned messages into xmpp. The basic form of networking is a single client connecting to a single server via TCP/IP, and then transferring XML over it.
  • Client: connect directly to the server through a TCP connection, and get all the functions provided by the server and the federated server through xmpp. Multiple different clients can log in at the same time and connect to a server concurrently, each client with different resources is distinguished by the resource identifier of the xmpp address.

2.2.3 The communication principle of XMPP

Instructions related to instant messaging are transmitted during XMPP communication. It's just that the form of the agreement becomes plain text in XML format.

  1. Client1 connects to the server;
  2. The server authenticates itself with a certificate in the local directory system;
  3. Client1 specifies the address of the target Client2, and let the server inform the target status;
  4. Server lookup, connection and mutual authentication;
  5. Clients (Client1 and Client2) interact.

The following is an example of an XMPP communication process:

A client (minstone) connects to an XMPP server (127.0.0.1:5222/TCP), sends a message "Bye Bye" to another client (test), and then logs out.

  • 1. The client (minstone) sends a connection request to the server

客户端:

<?xmlversion='1.0'?>
<stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" to="127.0.0.1">
  • 2. The server responds to the request

服务器:

<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'
from='127.0.0.1' id='1461777714'>
  • 3. The server uses the certificate of the local directory system to authenticate it

客户端:

<iq type="set" id="auth_2" to="127.0.0.1" >
<query xmlns="jabber:iq:auth">
  <username>minstone</username>
  <password>mypassword</password>
  <resource>Work</resource>
 </query>
</iq>
  • 4. The server returns the corresponding result to the client

服务器:

<iq from="127.0.0.1" id='auth_2' type='result'/>
  • 5. The user sends a message request and logs out

客户端:

<message to="[email protected]" >
    <subject>Bye Bye</subject>
    <body>test 1449</body>
</message>
<presence type="unavailable" >
    <status>Logged out</status>
</presence>
</stream:stream>
  • 6. The server returns logout information

服务器:

</stream:stream>

From the point of view of the document, all the XML texts sent by the client or server are concatenated together to <stream>form </stream>a complete XML document. The stream tag among them is the so-called XML Stream (XML stream). Those XML elements in <stream>the </stream>middle <message>...</message>are so-called XML Stanza (XML nodes). The basic mode of XMPP core protocol communication is to establish a stream first, and then negotiate a bunch of security and other things. The intermediate communication process is that the client sends XML Stanza, one by one. The server sends XML Stanza to the client according to the information sent by the client and the logic of the program. But this process is not a question-and-answer process, and it is possible to send a letter from one party to the other at any time. The final stage of communication is </stream>closing the stream.

Information: Basic concepts of xmpp

2.2.4 Advantages of XMPP protocol

  • Openness: The XMPP protocol is free, open, open, and easy to understand. And in terms of clients, servers, components, source code libraries, etc., there are already multiple implementations.
  • Standard: The Internet Engineering Task Force (IETF) has named Jabber's core XML streaming protocol as XMPP and officially listed it as an approved real-time communication and presence technology. The technical specifications of XMPP have been defined in RFC 3920 and RFC 3921. Any IM provider can connect with Google Talk under the XMPP protocol.
  • Proven to work: The first Jabber (now XMPP) technology was developed by Jeremie Miller in 1998 and is now fairly stable; hundreds of developers have worked on XMPP technology. There are tens of thousands of XMPP servers running on the Internet today, and millions of people use XMPP instant messaging software.
  • Distributed: The architecture of the XMPP network is very similar to that of email; the communication method of the XMPP core protocol is to create a stream first, and XMPP uses TCP to transmit XML data streams without a central master server. Anyone can run their own XMPP server, enabling individuals and organizations to take control of their real-time messaging experience.
  • Security: Any XMPP protocol server can be independent of the public XMPP network (for example, in the internal network of an enterprise), and the reliable security of technologies such as SASL and TLS is already included in the core XMPP technical specifications.
  • Extensibility: The power of the XML namespace allows anyone to build customized functions on the basis of the core protocol; in order to maintain transparency, common extensions include extension protocols that support audio and video transmission Jingle.
  • Good flexibility: In addition to being used in real-time communication applications, XMPP can also be used in network management, content contribution, collaboration tools, file sharing, games, remote system monitoring, etc.
  • Diversity: Companies and open source projects that use XMPP protocol to build and deploy real-time applications and services are distributed in various fields; The plight of being "kidnapped".

2.2.5 Protocol disadvantages of XMPP

  • Data load is too heavy: With the presence of usually more than 70% of the data traffic of the XMPP protocol server and nearly 60% of the data being repeatedly forwarded, the XMPP protocol currently has a large structure in which the data is provided to multiple recipients. New protocols are currently being studied to alleviate this problem.
  • No binary data: The way the XMPP protocol is encoded into a single long XML file, it cannot provide binary data. Therefore, the file transfer protocol uses the same external HTTP. If unavoidable, the XMPP protocol also provides for file transfers with encoding for all data using Base64.

3 Openfire related knowledge

3.1 Definition of Openfire

Openfire is an open source real-time collaboration (RTC) server developed based on Java, based on the XMPP (Jabber) protocol. You can use it to easily build high-efficiency instant messaging servers.

Openfire is very simple to install and use, and uses the Web for management. A single server can support tens of thousands of concurrent users. Because it adopts the open XMPP protocol, you can use various IM client software that supports the XMPP protocol to log in to the service.

Information:   official website    github address   Openfire Tutorial Network

3.2 Openfire installation and configuration

For the installation and configuration process, please refer to: Openfire Installation Tutorial

The configuration is done as follows:
insert image description here

3.3 Use Spark to log in to Openfire

Spark is an open source instant messaging tool developed in java. It can log in to an instant messaging server based on the XMPP protocol and communicate on it (it can be understood as a simplified version of QQ).
spark

For the login method, please refer to the above Openfire安装教程blog content, and will not repeat it here.

4 Knowledge about Smack

4.1 Definition of Smack

Smack is an open source, easy-to-use XMPP (jabber) client library. It encapsulates some operations in the instant messaging process for the XMPP protocol, such as login, registration, sending messages, etc., so that developers do not need to care about the processing of a large amount of xml data in XMPP. Spark is developed using smack.

github address

Disadvantages: API is not designed for a large number of concurrent users. Each client requires one thread, which takes up a lot of resources. Generally, one machine can only simulate limited (thousands) clients.

Note:

4.2 Example of Smack

The following is a smack login sample code:

XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
  .setXmppDomain("jabber.org")  //xmpp服务器域名
  .setHost("earl.jabber.org")  //xmpp服务器主机地址
  .setPort(8222)  //xmpp服务器端口
  .build();
AbstractXMPPConnection conn = new XMPPTCPConnection(config);
conn.connect();//建立连接
conn.login("username", "password");//用户登录

Smack has been updating and upgrading. The codes of different versions are very different, and the official documents are not detailed enough. The information found on the Internet is basically the old version of the code. This is a pit!

5 Epilogue

Interested students can go to github to see the xmpp sample application Android XMPP Demo written by me.

Guess you like

Origin blog.csdn.net/gs12software/article/details/90749465