Skip to content

Chat server connection

The Hive SDK chat feature uses WebSocket communication with the Hive chat socket server. Therefore, to use the Hive SDK chat feature, you must first refer to the guide below to connect to the Hive chat socket server.

Connection request

To connect to the chat, call the connect method of the Chat class.

Here is an example code that requests a chat connection.

Warning

Hive SDK chat feature can only be used by users (including guest accounts) who have logged in using the Hive SDK authentication feature and have been issued a PlayerID.

using hive;

Chat.connect((ResultAPI result) => {
    if (result.isSuccess()) {
        // API Call Success
    }
});
#include "HiveChat.h"

FHiveChat::Connect(FHiveChatOnResultDelegate::CreateLambda([this](const FHiveResultAPI& Result) {
    if (Result.IsSuccess()) {
        // API Call Success
    }
}));
#include <HIVE_SDK_Plugin/HIVE_CPP.h>
using namespace hive;

Chat::connect([=](ResultAPI const & result) {
    if (result.isSuccess()) {
        // API Call Success
    }
});
import com.hive.Chat;
import com.hive.ResultAPI;

Chat.connect(object : Chat.ConnectListener {
    override fun onResult(result: ResultAPI) {
        if (result.isSuccess) {
            // API Call Success
        }
    }
})
import com.hive.Chat;

Chat.connect(result -> {
    if (result.isSuccess()) {
        // API Call Success
    }
});
import HIVEService

ChatInterface.connect { result in
    if result.isSuccess {
        // API Call Success
    }
}
#import "HIVEService.h"

[HiveChat connectWithHandler:^(HIVEResultAPI * result) {
    if (result.isSuccess) {
        // API Call Success
    }
}];

Reconnection request

Note

In the case of a temporary network disconnection or (iOS only) during the transition between background and foreground, the SDK will automatically attempt to reconnect to the network once.

If the chat socket server connection is disconnected due to network conditions, you can request a chat reconnection. If you request a reconnection within 10 minutes after the chat socket server disconnection, it will attempt to automatically join the channel you were previously participating in. The result of the reconnection request will return a list of channels that were successfully joined and a list of channels that failed to join.

When requesting a chat reconnection, call the reconnect method of the Chat class. Here is an example code for requesting a chat reconnection.

using hive;

Chat.reconnect((ResultAPI result, List<String> channelIds, List<String> failChannelIds) => {
    if (result.isSuccess()) {
        // API Call Success
    }
});
#include "HiveChat.h"

FHiveChat::Reconnect(FHiveChatOnReconnectDelegate::CreateLambda([this](const FHiveResultAPI& Result, const TArray<FString>& ChannelIds, const TArray<FString>& FailChannelIds) {
    if (Result.IsSuccess()) {
        // API Call Success
    }
}));
#include <HIVE_SDK_Plugin/HIVE_CPP.h>
using namespace hive;

Chat::reconnect([=](ResultAPI const & result, std::vector<std::string>channelIds, std::vector<std::string>failChannelIds) {
    if (result.isSuccess()) {
        // API Call Success
    }
});
import com.hive.Chat;
import com.hive.ResultAPI;

Chat.reconnect(object : Chat.ReconnectListener {
    override fun onResult(result: ResultAPI, channelIds: ArrayList<String>, failChannelIds: ArrayList<String>) {
        if (result.isSuccess) {
            // API Call Success
        }
    }
})
import com.hive.Chat;

Chat.reconnect(result, channelIds, failChannelIds -> {
    if (result.isSuccess()) {
        // API Call Success
    }
});
import HIVEService

ChatInterface.connect { result, channelIds, failChannelIds in
    if result.isSuccess {
        // API Call Success
    }
}
#import "HIVEService.h"

[HiveChat reconnectWithHandler:^(HIVEResultAPI * result, NSArray<NSString *> * channelIds,  NSArray<NSString *> * failChannelIds) {
    if (result.isSuccess) {
        // API Call Success
    }
}];

Disconnection request

To disconnect the chat, call the Chat class's disconnect() method. Below is an example code for disconnecting the chat connection.

Note

The chat disconnection request is automatically executed when the app user account logs out.

using hive;

Chat.disconnect((ResultAPI result) => {
    if (result.isSuccess()) {
        // API Call Success
    }
});
#include "HiveChat.h"

FHiveChat::Disconnect(FHiveChatOnResultDelegate::CreateLambda([this](const FHiveResultAPI& Result) {
    if (Result.IsSuccess()) {
        // API Call Success
    }
}));
#include <HIVE_SDK_Plugin/HIVE_CPP.h>
using namespace hive;

Chat::disconnect([=](ResultAPI const & result) {
    if (result.isSuccess()) {
        // API Call Success
    }
});
import com.hive.Chat;
import com.hive.ResultAPI;

Chat.disconnect(object : Chat.DisconnectListener {
    override fun onResult(result: ResultAPI) {
        if (result.isSuccess) {
            // API Call Success
        }
    }
})
import com.hive.Chat;

Chat.disconnect(result -> {
    if (result.isSuccess()) {
        // API Call Success
    }
});
import HIVEService

ChatInterface.disconnect(result) {
    if result.isSuccess {
        // API Call Success
    }
}
#import "HIVEService.h"

[HiveChat disconnectWithHandler:^(HIVEResultAPI * result) {
    if (result.isSuccess) {
        // API Call Success
    }
}];

Check connection status

Checks if the app user is currently connected to the chat. Calls the Chat class isConnected method.

Here is an example code to check the chat connection status.

using hive;

Boolean isConnected = Chat.isConnected();
#include "HiveChat.h"

bool IsConnected = FHiveChat::IsConnected();
#include <HIVE_SDK_Plugin/HIVE_CPP.h>
using namespace hive;

bool isConnected = Chat::isConnected();
import com.hive.Chat;

val isConnected = Chat.isConnected()
import com.hive.Chat;

Boolean isConnected = Chat.isConnected();
import HIVEService

let isConnected = ChatInterface.isConnected()
#import "HIVEService.h"

bool isConnected = [ChatInterface isConnected];

Connection event management

The Hive chat socket server detects the connection status and continuously delivers status change events to the app. For detailed event handling methods, please refer to the Event Management > Connection Events document.