Implementation of extensible Messaging and Presence Protocol (XMPP)

Source: Internet
Author: User
Tags representational state transfer

This article transferred from: http://www.ibm.com/developerworks/cn/xml/x-xmppintro/#major1 

For more information, please refer to the original

Content Directory

    • XMPP Architecture
    • Address in XMPP
    • XMPP protocol
    • Example of XMPP using Ruby
    • Application of XMPP
    • Multi-lingual XMPP
    • Conclusion

Instant messaging (IM) is a popular application among both temporary Internet users and business users. It not only provides users with a way to communicate with others in real time, but also gets their presence information (online, off, offline, and so on). Jabber was one of the earliest open IM protocols developed by Jeremie Miller and appeared as a non-standard IM protocol in 1998. Because Jabber is an extensible protocol created using XML, it quickly creates other applications that can be used as a universal transport or message-oriented middleware (MoM). Eventually the XMPP was produced by Jabber, which is a standard-based protocol in the form of an IETF workgroup protocol: RFC 3920, "extensible Messaging and Presence Protocol (XMPP)".

XMPP is not the only generic message delivery protocol. Other popular protocols, such as XML-RPC and SOAP, can provide function calls (such as semantics) for this feature. Some newer methods, such as representational state Transfer (ReST), use URLs to specify locations, objects, and methods to provide controlled file access.

XMPP Architecture

XMPP is similar to other application-layer protocols, such as SMTP. In these schemas, a client with a unique name communicates with another client with a unique name through the associated server. Each client executes the client's protocol form, and the server provides routing functionality in the form. Figure 1 illustrates this simple architecture. In this example, each client is part of the same domain name (DISCOVERY.NASA.GUV).

The server can also communicate for routes between different domains, such as between Discovery.nasa.guv and Europa.nasa.guv. In addition, gateways can be used to convert between external messaging domains and protocols. The example in Figure 2 shows an XMPP network with a gateway to a short message service (SMS) domain and an SMTP domain. In this case, most of the gateways are used to convert between the IM protocol (for example, XMPP and Internet Relay Chat (IRC)). As an extensible protocol, XMPP is an ideal central protocol for providing unified connectivity between different endpoint protocols. The XMPP gateway allows terminating a given client-to-server session and initiating a new session to the target endpoint protocol (with the necessary protocol conversions at the same time).

Address in XMPP

The address in XMPP (that is, Jabber ID[JID]) is similar to a standard e-mail address, but with several notable differences. The JID contains an optional node, a domain, and an optional resource:

[Node "@"] domain ["/" resource]

The most common use is to define an IM user (like an email address), such as [email protected]. The user can log on to the XMPP server multiple times, and in this case the resource can represent the location. For example, a sample user might have a primary terminal ([email protected]/terminal) JID and another JID from the EVA pod ([email protected]/eva_pod1). Therefore, you can find a specific location, or leave the location and find the user, regardless of where the user is logged in.

XMPP protocol

XMPP is a relatively simple protocol that appears on a TCP socket through an XML message. Asynchronous communication occurs in an XML stream with an XML section (stanza). An XML stream is a container that encapsulates the exchange of XML information between two entities. XML streams pass XML sections, which are scattered units of information. For example, an XML section is used in XMPP to deliver messages (text between IM users) and on-farm information. To illustrate these concepts, consider a simple example of IM communication using XMPP between two clients.

Figure 3 illustrates a simple session between two entities. Note that at least one server will appear in the session (in this case, because two clients exist in the same domain, so to be exact, there is only one server). In Figure 3, the client on the left side is the initiating entity (initiating XMPP communication between two entities). The XML stream uses to attributes to identify the receiving domain (and to define the XML namespace). The receiving client on the right receives the XML stream and responds with an XML stream response (in this case, using from attributes). At this stage, several different negotiations (such as authentication and encryption) can be performed. Please ignore this part of the discussion (the exception is when IM clients appear in different domains for server-to-server communication). (See the text version of Figure 3.) )

The next step in the XML stream in Figure 3 is to deliver the message. This communication is carried out in the message section and includes the source and destination XMPP addresses ( from and to ), the language used, and the messages in the section body. The other party responds with its own message, the key difference being the source and destination XMPP addresses. Finally, a stream shutdown message (at both ends of the connection) is emitted to close the XML stream.

Any side can return an error, as defined below. In this example, the other party sends an invalid XML stream and a section.

<stream:error>  <xml-not-well-formed xmlns= ' urn:ietf:params:xml:ns:xmpp-streams '/><stream: Error>

Although this example demonstrates simple IM communication, it is easy to understand how the message section is converted to RPC messages while loading security issues from peer negotiation. You can register a function as a node to create a dynamic WEB service framework instead of registering users in the domain. Now let's look at how to create a simple application that communicates in XMPP.

Example of XMPP using Ruby

Select the XMPP Library

One of the other interesting aspects of XMPP is that you can choose from a large number of libraries, including a wide variety of languages. This example uses the Ruby language and the XMPP4R library. For links to the various existing libraries, see resources.

Use the library to demonstrate XMPP and explore the development of a simple IM proxy used as a technical dictionary. Using this method, when you enter a word through a standard Instant messaging program, the IM agent returns its definition.

This example implements an IM proxy that can be connected to another IM agent through XMPP, and once the connection is successful, the definition of the word is interpreted. Listing 1 provides a simple XMPP proxy.


Listing 1. Simple XMPP proxy for text definition

Require ' xmpp4r/client ' # Create a *very* simple dictionary using a hashhash = {}hash[' ruby '] = ' greatest Little Object ori ented scripting language ' hash[' xmpp4r '] = ' simple XMPP library for Ruby ' hash[' XMPP '] = ' extensible Messaging and Presence Protocol ' # Connect to the server and Authenticatejid =jabber::jid::new(' [email protected]/home ') cl =jabber::client::new(Jid) Cl.ConnectAllAuth(' Password ') # indicate our presence to the SERVERCL.send Jabber::P resence::new# Send A salutation to a given user that we ' re readysalutation =jabber::message::new(' [email protected] ', ' Dictbot ready ') salutation.Set_type(: Chat).set_id(' 1 ') cl.SendSalutation # ADD A message callback to respond to peer REQUESTSCL.Add_message_callbackDo |inmsg| # Lookup The word in the dictionary resp = hash[inmsg.body] if resp = = Nil resp = "don ' t know about" + inmsg.b Ody End # Send the response outmsg =jabber::message::new(Inmsg.from, RESP) outmsg.Set_type(: Chat).set_id(' 1 ') cl.Sendoutmsgend# Runwhile 1end

Listing 1 first creates a simple dictionary. To do this, you can use classes in Ruby hash , which allow you to create key-value pairs (similar to arrays), but you can then easily refer to them by key. Next, connect to the server using the XMPP4R library. First use Client the class to create a JID and a new client connection. To really connect to an IM server, use the connect method. Once connected, you can use a password to invoke the auth method. Now, the connection can be used to deliver the message.

The next step (optional) is to indicate that you are already logged on to the IM server. To do this, you need to send a presence section to the server. You can also send an optional message to each other and tell the other that you are online. This can be done by creating a message section and initializing it with the opposite address and message. After the message is initialized successfully, you can Client send it by using a method on the class instance send .

To respond to messages sent to you, use the method of your client connection add_message_callback . Any time a message arrives, a code block is called to process the message. An incoming message is represented as inmsg (an Message instance). The first thing to do is check to see if the word in the incoming message body definition is in your dictionary. If it returns nil , it means that the word is not found, so you need to provide a default response. inmsg.fromcreates a new message using the incoming message () and the response string. After initialization is complete, the new message is sent to the sender through the client instance.

Figure 4 shows one run of the application. This example uses the popular Pidgin universal chat client. The pidgin client supports all major chat protocols and can be used with many existing chat networks (even synchronously). Figure 4 shows the message delivery pop-up window that is created when the IM agent connects to the server and defines the user to start the session.


Figure 4. sample IM sessions using IM proxy

This application was originally very simple, but xmpp4r provides many classes and methods for other features such as account registration, discovery, file migration, multi-user chat, publish/subscribe, and even RPC. You can find a "browsable" class API in resources that provides a convenient way to view all the xmpp4r files, classes, and methods.

Back to top of page

Application of XMPP

XMPP provides a common framework for messaging in the network. In addition to the traditional IM and presence data distributions, XMPP can also be used for many different purposes.

Closed applications for IM include group or multi-party messaging, or development of multi-user chat rooms. With multi-party communication, you can achieve similar features as Twitter-provided micro-blogging (micro-blogging). But text is not the only data that can be transmitted through XMPP. Other forms of communication include sound, picture, and video data.

Service discovery protocols (such as Bonjour or service location Protocol) have emerged today, but XMPP provides a solid foundation for service discovery and the promotion of services and features in the network.

Online games are heavily used in XMPP. XMPP provides a set of key features for online gaming, including near real-time communication of authentication, presence information, chat, and extensible game state information.

Finally, XMPP is a perfect deal for the new era of cloud computing. Cloud computing and storage systems rely on different levels and forms of communication, not only for messaging across systems to relay state, but also for migrating large objects such as storage or virtual machines. By combining data protection in authentication and transmission, XMPP can be applied at different levels and can be used as an ideal middleware protocol.

Please note that most applications do not have any relationship with human communication, but are related to machine communication (MMI or machine-to-machine communication). When you find that the protocols for IM are used for a variety of purposes, you'll find this a very interesting thing to do.


Multi-lingual XMPP

XMPP is implemented as a set of libraries, providing XMPP capabilities to the application. With the support of many languages from XMPP, it's easy to tell that XMPP is a very useful protocol. You will find that the XMPP library software supports traditional languages such as C and, C++ as well as popular scripting languages such as Ruby, Java™ language, Python, Perl, and TCL. You will also find that the XMPP library supports other languages such as Erlang, C# and Lisp. Therefore, regardless of the environment in which you are located, it is possible to find an XMPP library to access XMPP. For a list of languages supported by the various XMPP libraries, see resources.

Back to top of page

Conclusion

The growing ReST

Although ReST is an architectural model rather than an implementation method, it grows fast in some areas. Rest's simple model for remote resource management has been stored in cloud storage, where rest is used as a storage access and management model.

Many useful techniques are often used in new ways that the technology creators have never considered before. For example, HTTP is a de facto standard protocol that serves Web pages on the Internet, but it is also used as an application-layer transport for other protocols such as SOAP and XML-RPC, including protocol models such as ReST. XMPP is another useful technique for discovering many new applications beyond IM. How will you refer XMPP to your solution?

Implementation of extensible Messaging and Presence Protocol (XMPP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.