Skip to content

Send first chat message

The ConnectyCube Chat API is a set of tools that enables developers to integrate real-time messaging into their web and mobile applications. With this API, users can build powerful chat functionalities that support one-on-one messaging, group chats, typing indicators, message history, delivery receipts, and push notifications.

If you’re planning to build a new app, we recommend starting with one of our code samples apps as a foundation for your client app.
If you already have an app and you are looking to add chat to it, proceed with this guide. This guide walks you through installing the ConnectyCube SDK in your app, configure it and then sending your first message to the opponent in 1-1 chat.

Before you start

Before you start, make sure:

  1. You have access to your ConnectyCube account. If you don’t have an account, sign up here.
  2. An app created in ConnectyCube dashboard. Once logged into your ConnectyCube account, create a new application and make a note of the app credentials (app ID, auth key, and auth secret) that you’ll need for authentication.

Step 1: Configure SDK

To use chat in a client app, you should install, import and configure ConnectyCube SDK.

Note: If the app is already created during the onboarding process and you followed all the instructions, you can skip the ‘Configure SDK’ step and start with Create and Authorise User.

Install SDK

To connect ConnectyCube to your iOS app just add it into your Podfile which is a part of CocoaPods tool:

platform :ios, '9.0'
use_frameworks!
target 'MyApp' do
pod 'ConnectyCube'
end

and then install packages from the command line:

Terminal window
pod install

Import SDK

Add the following import statement to start using all classes and methods.

import ConnectyCube

Initialize SDK

Initialize the SDK with your ConnectyCube application credentials. You can access your application credentials in ConnectyCube Dashboard:

ConnectyCube().doInit(applicationId: APP_ID, authorizationKey: AUTH_KEY, authorizationSecret: AUTH_SECRET, connectycubeConfig: nil)

Step 2: Create and Authorise User

To make API requests, firstly you need to create and authenticate a user who will send/receive messages in chat later.

Create an application session

ConnectyCube.createSession({ session -> }, { error -> })

Register a user

let user: ConnectycubeUser = ConnectycubeUser()
user.login = "marvin18"
user.password = "supersecurepwd"
user.email = "awesomeman@gmail.com"
user.fullName = "Marvin Simon"
user.phone = "47802323143"
user.website = "https://dozensofdreams.com"
user.tags = "iphone, apple"
ConnectyCube().signUp(user: user, successCallback: {(user) in
}) { (error) in
}

Create User’s session

To allow users to proceed with any actions within the application, you need to create a session for them. Having created an application session, you need to upgrade it to a user session by calling logIn method:

ConnectyCube().signIn(user: userToLogin, successCallback: {(user) in
}) { (error) in
}

Now that the user is authorized in the application, they can proceed with chat creation and messaging with opponent(s).

Step 3: Connect User to chat

Connecting to the chat is an essential step in enabling real-time communication. By establishing a connection, the user is authenticated on the chat server, allowing them to send and receive messages instantly. Without this connection, the app won’t be able to interact with other users in the chat.

let user = ConnectycubeUser()
user.id = 2746
user.password = "supersecurepwd"
ConnectyCube().chat.login(user: user, successCallback:{
}, errorCallback: { error in
}, resource: ConnectycubeSettings().chatDefaultResource)

Use ConnectycubeConnectionListener to handle different connection states:

class YourClass : NSObject {
override init() {
super.init()
ConnectyCube().chat.addConnectionListener(listener: self)
}
}
extension YourClass : ConnectycubeConnectionListener {
func onConnected() {
}
func onDisconnected() {
}
}

Step 4: Create 1-1 chat

Creating a 1-1 chat is essential because it gives a unique conversation ID to correctly route and organize your message to the intended user.

let dialog = ConnectycubeDialog()
dialog.type = ConnectycubeDialogType.companion.PRIVATE
dialog.occupantsIds = [34] // an ID of opponent
ConnectyCube().createDialog(connectycubeDialog: dialog, successCallback: {(dialog) in
}) { (error) in
}

Step 5: Send / Receive messages

Once the 1-1 chat is set up, you can use it to exchange messages seamlessly. This code example demonstrates how to send and receive messages in the created 1-1 chat:

let message = ConnectycubeMessage()
message.body = "How are you today?"
message.recipientId = 34
ConnectyCube().chat.sendMessage(msg: message, successCallback: {
}) { (error) in
}
//MARK: ConnectycubeMessageListener
ConnectyCube().chat.addMessageListener(listener: self)
extension YourClass: ConnectycubeMessageListener {
func onMessage(message: ConnectycubeMessage) {
}
func onError(message: ConnectycubeMessage, ex: KotlinThrowable) {
}
}

That’s it! You’ve mastered the basics of sending a chat message in 1-1 chat in ConnectyCube.

What’s next?

To take your chat experience to the next level, explore ConnectyCube advanced functionalities, like adding typing indicators, using emojis, sending attachments, and more.

Follow the Chat API documentation to enrich your app and engage your users even further!