Conversation SDK for JavaScript
Deprecated (Moved to nexmo-conversation) Please npm install nexmo-conversation
instead
Quick Steps
Setup
$ npm install nexmo-conversation
create a nexmo app using the nexmo-cli
$ nexmo app:create "Application name" --keyfile private.key
get a token to create a user:
$ nexmo jwt:generate private.key application_id=[application_id]
create a user in your app
$ curl -X POST -H 'Authorization: Bearer [JWT]' -H 'Content-Type:application/json' -d '{"name":"[username]"}' https://api.nexmo.com/beta/users
include the script in your web page
<script src="node_modules/nexmo-conversation/dist/conversationClient.js"></script>
get a user's token
$ nexmo jwt:generate private.key application_id=[application_id] sub=[username]
Create an instance and login
var rtc = new ConversationClient({debug:false});
rtc.login(token).then(
function(application){
console.log(application.conversations);
});
Enable notifications
rtc.notifications({
state: "on",
dir: "auto",
lang: "EN",
tag: "notificationPopUp",
icon: "https://int-dir.s3.amazonaws.com/uploads/282_282_social_icon_360x360.png"
});
Show a notification
rtc.notify("Title", "Message");
Create a new Conversation
var conversationData = {name:'My Nexmo Conversation'};
application.newConversation(conversationData).then(
function(conversation) {
conversation.join(application.me).then(
function(member) {
console.log("Joined as " + member.name);
}).catch(
function(error) {
console.log(error);
});
}).catch(function(error) {
console.log(error);
});
Get a conversation you are a member
application.getConversation(conversation_id).then(
function(conv) {
conversation = conv;
});
Set up Text listener for incoming Text Events
conversation.on("text", function(sender, textEvent){
if (textEvent.cid === conversation.id){
console.log("my message was:", textEvent, sender);
}else{
console.log("got a message from another member:", textEvent, sender);
}
});
Sending a Text Event
conversation.sendText("Hi Nexmo").then(
function(){
console.log('message was sent');
}).catch(function(error){
console.log('error sending the message', error);
});
Sending an Image
conversation.sendImage(fileInput.files[0]);
Setup an ImageEvent listener and download the image file
conversation.on("image", function(sender, imageEvent){
imageEvent.fetchImage().then(function(imagedata) {
message.body.thumbnail = imagedata;
});
}
Build
$ npm install
$ cd node_modules/nexmo-conversation
$ grunt
![NEXMO](https://www.nexmo.com/wp-content/themes/nexmo/resources/img/nexmo_logo_157x43_fullcolor.png)