Send a message
Update time: 2023/09/27 03:08:27
Instant Messaging (IM) provides a complete set of instant messaging essential capabilities. You can build diverse features for your messaging apps using IM.
This document describes how to implement peer-to-peer (P2P) encrypted messaging messaging by calling APIs in the integrated NetEase IM SDK (NIM SDK).
NIM SDK
NIM SDK offers a full-fledged instant messaging capabilities for multiple platforms, such as Windows, macOS, Web, Node.js, Mini Program for Wechat. NIM SDK wraps internal complexities, and provides easy-to-use APIs to facilitate quick integration with your applications.
NIM SDK is compatible with browsers, such as Internet Explorer 9, Edge, Chrome 58, Safari 10, Firefox 54 and more, Safari of iPhone 5s and later (operating system iOS 8.0 or later) and its built-in WeChat browser, Chrome with Android 5 or later and its built-in WeChat browser.
Prerequisites
- You have created an application in the CommsEase console and get the AppKey of your project.
- You have registered IM accounts and get accid and token.
You can implement messaging for P2P chats by performing the following 3 steps.
The image below may not be displayed due to network issues. To fix the display problem, refresh the page.
Step 1: Integrate NIM SDK
Install the npm package by running the following command.
shellnpm install @yxim/nim-web-sdk@latest
- IM SDK is compatible with IE9 (v5.0.0 or earlier supports IE8). IE8 or IE9 must use HTTPS to connect to CommsEase servers. Other types of browsers can connect to the CommsEase IM server using HTTP or HTTPS.
- The interfaces provided by NIM SDK for cross-platform capabilities are called similar to the JavaScript calling method in browsers. NIM SDK is also compatible with unique features provided by each platforms, such as database, upload, and WebSocket restrictions. For more information, see Add web dependencies.
Step 2: Initialize SDK and login
After integrating NIM SDK into your project, you must initialize the SDK before using the capabilities provided.
When the SDK is initialized, the listeners to track messaging are registered and the account is logged in.
- The
account
is the IM account accid andtoken
is the IM account token。 - The
onxxxx
in the example indicates events. After registering the listener in the initialization, the callback will be triggered later. For the initialization parameters of NIM SDK, seegetInstance
.
Sample code:
javascriptvar nim = NIM.getInstance({
debug: true, // Specify whether to enable logging and print log records in the console. Enable this option in the development phase.
appKey: 'appKey',
account: 'account',//used for login
token: 'token',//used for login
db:true, //If you don't want to enable the database, set to false. The default value is true.
// privateConf: {}, // On-premises deployment configuration
onconnect: onConnect,
onwillreconnect: onWillReconnect,
ondisconnect: onDisconnect,
onerror: onError
//Events for message sending and receiving
onroamingmsgs: onRoamingMsgs,
onofflinemsgs: onOfflineMsgs,
onmsg: onMsg
});
function onMsg(msg) {
console.log('Message received', msg.scene, msg.type, msg);
pushMsg(msg); // You must implement pushMsg to insert the message into your own data
switch (msg.type) {
case 'custom':
onCustomMsg(msg);
break;
case 'notification':
onTeamNotificationMsg(msg);
break;
// Other case
default:
break;
}
}
Step 3: Send text messages
This section describes how to send and receive text messages between User A and User B in a P2P chat scenario using NIM SDK.
- User A sends a message to User B.
NIM SDK allows you to send or receive multiple types of messages, such as image, audio, video, file, location, alerts, notifications, and custom messages. For more information, see Messaging.
The sample code for sending text messages:
javascriptvar msg = nim.sendText({
scene: 'p2p',
to: 'account',
text: 'hello',
done: sendMsgDone,
});
console.log('Sending P2P text messages, id=' + msg.idClient);
function sendMsgDone(error, msg) {
console.log(error);
console.log(msg);
console.log(
'Send ' +
msg.scene +
' ' +
msg.type +
'message' +
(!error ? 'success' : 'failure') +
', id=' +
msg.idClient
);
}
-
User B receives a message for the
msg
event. -
(Optional) User A logs out and destroys the instance.
Sample code:
javascript// Disconnect from server
nim.disconnect();
What’s next
To ensure communication security, replace the IM accounts you created in the CommsEase console for use for testing with production-ready IM accounts created by calling Server APIs.