Skip to content

Java Client API

The Java API supports both standard Java applications and Android apps, using JDK 17 or higher.

The API is distributed as .jar files which include the main API and dependancies. They can be easily added to your Java project in the IDE of your choice.

API documentation

Consult the API Javadoc

Adding the API in IntelliJ IDEA

After you have created a new Java project proceed with the following steps:

  • create a lib/ folder in your project's main folder and copy all the API .jar files in it;
  • in the Project pane right-click the top element and choose Open Module Settings...;
  • under Project Settings click Modules;
  • there are three tabs in this view: Sources, Paths, Dependencies. Makes sure Dependencies is selected;
  • click the plus icon, choose JARs or Directories... and select the lib/ folder from your project directory;
  • click Apply.

Adding the API in Eclipse

After you have created a new Java project proceed with the following steps:

  • create a lib/ folder in your project's main folder and copy all the API .jar files in it;
  • from the Package Explorer right click the icon of your project and choose Open Module Settings;
  • in the new window select Java Build Path;
  • select the Libraries tab and click the Add JARs... button;
  • navigate to the lib/ folder and select all the .jar files it contains;
  • close the various dialogue boxes.

Basic example

The following is a simple Java client that connects to SmartFoxServer running on the local machine:

import sfs3.client.*;
import sfs3.client.requests.*;
import sfs3.client.util.ConfigData;

public class SFS3Connector
{
    SmartFox sfs;
    ConfigData cfg;

    public SFS3Connector()
    {
        // Configure client connection settings
        cfg = new ConfigData();
        cfg.host = "localhost";
        cfg.zone = "Playground";

        // Set up event handlers
        sfs = new SmartFox();
        sfs.addEventListener(SFSEvent.CONNECTION, this::onConnection);
        sfs.addEventListener(SFSEvent.CONNECTION_LOST, this::onConnectionLost);
        sfs.addEventListener(SFSEvent.LOGIN, this::onLogin);
        sfs.addEventListener(SFSEvent.LOGIN_ERROR, this::onLoginError);

        System.out.println("API Ver: " + sfs.getVersion());

        // Connect to the server
        sfs.connect(cfg);
    }

    // ----------------------------------------------------------------------
    // Event Handlers
    // ----------------------------------------------------------------------

    private void onConnection(ApiEvent evt)
    {
        var success = (Boolean) evt.getParam(EventParam.Success);

        if (success)
        {
            System.out.println("Connection success");

            // Login as guest
            sfs.send(new LoginRequest(""));
        }
        else
            System.out.println("Connection Failed. Is the server running?"); 
    }

    private void onConnectionLost(ApiEvent evt)
    {
        System.out.println("Connection was lost");
    }

    private void onLogin(ApiEvent evt)
    {
        System.out.println("Logged in as: " + sfs.getMySelf().getName());
    }

    private void onLoginError(ApiEvent evt)
    {
        var message = (String) evt.getParam(EventParam.ErrorMessage);
        System.out.println("Login failed. Error: " + message);
    }
}