Connecting to the XMPP server

In this section

Establishing the server connection

The application must connect to port 5222 on the server

If port 5222 is not available, port 443 can be used instead; for this port, an encrypted connection is required, so the server connection must start with a TLS handshake (step 3).

Steps for connecting to the XMPP server:

  1. Handshake.

    The application requests to create a stream:

    <?xml version="1.0"?>
    <stream:stream xmlns:stream="" version="1.0" xmlns="jabber:client" to="" xml:lang="en" xmlns:xml="">

    The server returns the stream ID and information about available features (encryption, ZLIB compression, and SASL authorization):

    <?xml version='1.0'?>
    <stream:stream xmlns='jabber:client' xmlns:stream='' id='4235063168' from='' version='1.0' xml:lang='en'>
      <starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>
      <compression xmlns="">
      <mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>
  2. Establishing an encrypted connection (TLS).

    The application sends the encryption request stanza:

    <starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>

    The server confirms the encrypted connection:

    <proceed xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>
  3. TLS handshake. The application repeats the request to create a stream (see step 1), using an encrypted connection.

  4. If necessary, the application may request to compress the stream in ZLIB format. In the XMPP protocol, compression is described as an extension of the standard protocol: XEP-0138.
  5. Authorization. The application passes the server a Base64-encoded authorization token. The token must be formed from the user's login and the OAuth token, separating them with a zero byte, for example: test\00c4181a7c2cf4521964a72ff57a34a07.

    <auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="X-YANDEX-OAUTH">dGVzdABjNDE4MWE3YzJjZjQ1MjE5NjRhNzJmZjU3YTM0YTA3</auth>

    When authorization is successful, the server responds with:

    <success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>
  6. Authorized handshake. The application requests to create a stream (see step 1). The server lists the features that are available after authorization.

  7. Setting up the resource. The application passes the resource name in the following stanza:

    <iq type="set" id="bind_1">
      <bind xmlns="urn:ietf:params:xml:ns:xmpp-bind">

    The server returns the JID assigned to the application, which should be used in the following requests:

    <iq xmlns="jabber:client" type="result" id="bind_1">
      <bind xmlns="urn:ietf:params:xml:ns:xmpp-bind">
  8. Opening a session. The application requests to open a session:

    <iq type="set" id="session_1">
      <session xmlns="urn:ietf:params:xml:ns:xmpp-session"/>

    The server responds with the result of opening the session:

    <iq type="result" id="session_1">
    <session xmlns="urn:ietf:params:xml:ns:xmpp-session"/>
  9. Request for the client version (XEP-0092).

    After the session is opened, the server requests the version of the client application:

    <iq from="" type="get" to="" id="ask_version">
    <query xmlns="jabber:iq:version"/>

    The application should return its own name, the application version number and, optionally, the OS version:

    <iq type="result" to="" id="ask_version">
      <query xmlns="jabber:iq:version">
        <os>Mac OS X</os>

After the connection has been established, the server periodically checks the connection with the application by sending the following types of requests:

<iq from="" type="get" to="" id="ping_1">
  <ping xmlns="urn:xmpp:ping"/>

The application should respond in the following way, copying the value of the id attribute from the server request to its response:

<iq type="result" to="" id="ping_1"/>

Closing a connection

When finished, the application should close the XMPP stream by sending the server the closing string: