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 a 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
Section titled “Before you start”Before you start, make sure:
- You have access to your ConnectyCube account. If you don’t have an account, sign up here.
- 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 and auth key) that you’ll need for authentication.
Step 1: Configure SDK
Section titled “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
Section titled “Install SDK”React, Angular, Vue etc.
Section titled “React, Angular, Vue etc.”Install package from the command line:
npm install connectycube --save
yarn add connectycube
Plain HTML
Section titled “Plain HTML”Сonnect SDK js file as a normal script:
<script src="https://cdn.jsdelivr.net/npm/connectycube/dist/connectycube.min.js"></script>
If you use Vite build tool, update vite.config.ts
and add the following:
export default defineConfig({ plugins: ... esbuild: ...
define: { // add this line for ConnectyCube to work properly global: {}, },})
Also, it needs to add polyfils for some node libs.
Install the package as a dev dependency:
npm install --save-dev vite-plugin-node-polyfills
yarn add --dev vite-plugin-node-polyfills
Add the plugin to your vite.config.ts file:
import { defineConfig } from 'vite'import { nodePolyfills } from 'vite-plugin-node-polyfills'
// https://vitejs.dev/config/export default defineConfig({ plugins: [ nodePolyfills(), ],})
For Vite 6 to work, add the following code in package.json:
"overrides": { "vite-plugin-node-polyfills": { "vite": "^6.0.0" }}
Remix SSR (Server-Side Rendering) guide
Section titled “Remix SSR (Server-Side Rendering) guide”When using the ConnectyCube SDK in SSR environments like Remix, native Node.js modules such as “events” may cause runtime errors due to differences between Node and browser environments.
To ensure smooth SSR integration, follow the steps below.
Install ConnectyCube SDK and apply SSR patches:
npm install connectycubenpx connectycube patch-ssr # apply SSR patches# npx connectycube revert-ssr # revert SSR patches (if needed)
yarn add connectycubeyarn connectycube patch-ssr # apply SSR patches# yarn connectycube revert-ssr # revert SSR patches (if needed)
Remix Config Update
Section titled “Remix Config Update”Add the following option in your remix.config.js or remix.config.ts file to polyfill the “events” module, which is required by ConnectyCube’s dependencies:
/** * @type {import('@remix-run/dev').AppConfig} */const commonConfig = { // ... your existing config options ... browserNodeBuiltinsPolyfill: { modules: { events: true, }, },};
export default remixConfig;
This enables polyfilling of the Node.js “events” module in the browser environment, preventing errors related to “@xmpp/events” and similar packages used internally by ConnectyCube SDK.
Import SDK
Section titled “Import SDK”Add the following import statement to start using all classes and methods.
import ConnectyCube from 'connectycube';
*this is not needed for Plain HTML
Initialize SDK
Section titled “Initialize SDK”Initialize framework with your ConnectyCube application credentials. You can access your application credentials in ConnectyCube Dashboard:
const CREDENTIALS = { appId: 21, authKey: "hhf87hfushuiwef",};
ConnectyCube.init(CREDENTIALS);
const CREDENTIALS = { appId: 21, authKey: "hhf87hfushuiwef", authSecret: "jjsdf898hfsdfk",};
ConnectyCube.init(CREDENTIALS);
Step 2: Create and Authorise User
Section titled “Step 2: Create and Authorise User”As a starting point, the user’s session token needs to be created allowing to send and receive messages in chat.
const userCredentials = { login: "marvin18", password: "supersecurepwd" };
ConnectyCube.createSession(userCredentials) .then((session) => {}) .catch((error) => {});
Note: With the request above, the user is created automatically on the fly upon session creation using the login (or email) and password from the request parameters.
Important: such approach with the automatic user creation works well for testing purposes and while the application isn’t launched on production. For better security it is recommended to deny the session creation without an existing user.
For this, set ‘Session creation without an existing user entity’ to Deny under the Application -> Overview -> Permissions in the admin panel.
Step 3: Connect User to chat
Section titled “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.
const userCredentials = { userId: 4448514, password: "supersecurepwd",};
ConnectyCube.chat .connect(userCredentials) .then(() => { // connected }) .catch((error) => {});
Step 4: Create 1-1 chat
Section titled “Step 4: Create 1-1 chat”Creating a 1-1 chat gives a unique conversation ID to correctly route and organize your message to the intended user.
You need to pass type: 3
(1-1 chat) and an id of an opponent you want to create a chat with:
const params = { type: 3, occupants_ids: [56],};
ConnectyCube.chat.dialog .create(params) .then((dialog) => {}) .catch((error) => {});
Step 5: Send / Receive chat messages
Section titled “Step 5: Send / Receive chat messages”Once the 1-1 chat is set up, you can use it to exchange messages seamlessly. The code below demonstrates how to send and receive messages within a specific 1-1 chat.
const dialog = ...;const opponentId = 56;const message = { type: dialog.type === 3 ? 'chat' : 'groupchat', body: "How are you today?", extension: { save_to_history: 1, dialog_id: dialog._id }};
message.id = ConnectyCube.chat.send(opponentId, message);
// ...
ConnectyCube.chat.onMessageListener = onMessage;
function onMessage(userId, message) { console.log('[ConnectyCube.chat.onMessageListener] callback:', userId, message)}
Congratulations! You’ve mastered the basics of sending a chat message in ConnectyCube.
What’s next?
Section titled “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!