1
votes

I am designing a chat application using ejabberd as XMPP server and Smack 4.1 API. Below is a code snippet for managing connection.

    // Create a connection to the server.com server on 5222 port.
    XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
      .setUsernameAndPassword("user", "password")
      .setServiceName("server.com")
      .setHost("server.com_ip")
      .setPort(5222)
      .setSecurityMode(SecurityMode.disabled)
      .build();
    XMPPTCPConnection conn = new XMPPTCPConnection(config);
    try {
        conn.connect();
        System.out.println("Connection established.");
        conn.login();
        System.out.println("Logged in.");
    } catch (SmackException | IOException | XMPPException e) {
        System.out.println("Connection not established: " + e.getMessage());
    }

Some processing for chat and muc.

    // Disconnect
    conn.disconnect();
    System.out.println("Connection Closed.");

My Requirement:

  • Once a user logs into the app, they might not log out for months at a time. Exactly the way Whatsapp works.

My question is:

  • Is it a good idea to keep the connection open as long as user is logged in?
  • If not then is it a good idea to open and close the connection for every chat message?

Need Suggestion:

  • What is the most efficient way to handle a connection with XMPP server?
1
i have similar questions myself. Iam making a prototype instant messaging app with openfire xmpp server. coming from a place of no knowledge whatsoever id say the best way to handle connections would be to close the connection after a certain period if not in use.filthy_wizard
brother any updates in your issue? actually Im facing the issue related to establishing connection with xmpp @vinodRadhey
1. use Android services for keeping the connection alive all the time, or 2. keep the connection alive if app is open and use push notifications if app is closed.Vinod Patel

1 Answers

2
votes

Although the question is different, this SO I answered might help (comments too), but in a nutshell:

Opening and closing the connection for each and every message is Not what you should do.

Regarding keeping the connection Idle (like WhatsApp), it's one of the options (look into Services), the other would be to connect when your application comes up and stay connected as long as it's up, and use push notifications when the app is not running.

Another option would be a combination of both (more close to what WhatApp does).

Hope This Helps.