Initialization

Prev Next

Available in Classic and VPC

Initialization

Before using Ncloud Chat, it must be initialized. Add the project ID you checked in the dashboard. To initialize Ncloud Chat, follow these steps:

  1. Access the dashboard and check the project ID in the settings menu.
  2. Use the following code to initialize instances.

Javascript

  • Creates an instance of Ncloud Chat.
const nc = new ncloudchat.Chat();
  • Initializes Ncloud Chat with the project ID.
nc.initialize([PROJECT_ID]);
Parameter Type Description Required
PROJECT_ID string ID (Ncloud Chat dashboard Project ID) O

Typescript

  • Imports Ncloud Chat module.
import * as ncloudchat from "ncloudchat";
  • Creates an instance of Ncloud Chat.
const nc = new ncloudchat.Chat();
  • Initializes Ncloud Chat with the project ID.
nc.initialize([PROJECT_ID]);
Parameter Type Description Required
PROJECT_ID string ID (Ncloud Chat dashboard Project ID) O

Android (Kotlin)

  • Imports NChat module.
import com.nbase.sdk.NChat
  • Initializes NChat.
    Enter settings for projectId, region, and language.
NChat.initialize(this, [PROJECT_ID], [REGION], [LANGUAGE]) { data, e ->
     if (e != null) {
        // Handles error if initialization fails.
     } else {
        // Performs necessary actions upon successful initialization.
     }
}
Parameter Type Description Required
PROJECT_ID String ID (Ncloud Chat dashboard Project ID) O
REGION String Region (use "kr" if not using a specific one) O
LANGUAGE String Language code ("en", "ko", etc.) O

iOS (Swift)

  • Imports NChat module.
import NChat

class ChatViewController: ConnectDelegate {
    func onConnect() {
    }
    func onDisconnect() {
    }
    func onMessage(data: Any) {
    }
    ...
}
  • Specifies the class that will receive events.
let chatViewController = ChatViewController()
NChat.setDelegate(delegate: chatViewController)
  • Initializes NChat.
    Enter settings for projectId, region, and language.
NChat.initialize(projectId: [PROJECT_ID], region: [REGION], language: [LANGUAGE]) { result in
    switch(result)
    {
    case .success(let status):
        // Performs necessary actions upon successful initialization.
        break;
    case .failure(let error):
        // Handles error if initialization fails.
        break;
    }
}
Parameter Type Description Required
PROJECT_ID String ID (Ncloud Chat dashboard Project ID) O
REGION String Region (use "kr" if not using a specific one) O
LANGUAGE String Language code ("en", "ko", etc.) O

iOS (Objective-C)

  • Uses the NChat bridge module.
#import "NChatObjCSample-Swift.h"
  • Initializes NChat.
    Enter settings for projectId, region, and language.
[NChatBridge.shared initializeWithProjectId:[PROJECT_ID] region:[REGION] language:[LANGUAGE] :^(NSNumber * _Nullable result, NSError * _Nullable error) {
    if (error) {
        // Handles error if initialization fails.
    } else {
        // Performs necessary actions upon successful initialization.
    }
}];
Parameter Type Description Required
PROJECT_ID NSString* ID (Ncloud Chat dashboard Project ID) O
REGION NSString* Region (use "kr" if not using a specific one) O
LANGUAGE NSString* Language code ("en", "ko", etc.) O

Unity

  • Imports NBaseSDK module.
using NBaseSDK;
  • Creates an instance of NBaseSDK Chat.
NBaseSDK.Chat nc = NBaseSDK.Chat.GetInstance();
  • Initializes Ncloud Chat with the project ID, region, and language code.
nc.initialize([PROJECT_ID], [REGION], [LANGUAGE]);
Parameter Type Description Required
PROJECT_ID string ID (Ncloud Chat dashboard Project ID) O
REGION string Region (use "kr" if not using a specific one) O
LANGUAGE string Language code ("en", "ko", etc.) O

Error handling

  • Basic error handling is to add the code inside the try... catch as follows:

Javascript/Typescript

try
{
    // Write the code that may cause an error here.
    ...
} 
catch(e)
{
    // This is executed if an error occurs.
    console.log(e);
}

iOS

switch(result)
{
case .success(let status):
    // This is executed upon successful initialization.
    break;
case .failure(let error):
    // This is executed if initialization fails.
    break;
}

Unity

try
{
    // Write the code that may cause an error here.
    ...
} 
catch (InvalidOperationException e)
{
    // Write handling of specific error types here.
    Console.WriteLine("InvalidOperationException: {0}", e.Message);
}
catch(Exception e)
{
    // Write handling of general errors here.
    Console.WriteLine("Error: {0}", e.Message);
}