0.1.21 • Published 7 years ago

nexmo-conversation v0.1.21

Weekly downloads
159
License
To Be Decided
Repository
github
Last release
7 years ago

Deprecated (Moved to nexmo-stitch)

Please npm install nexmo-stitch instead

Conversation SDK for JavaScript

Quick Steps

Setup

$ npm install nexmo-conversation

create a nexmo application using the nexmo-cli

$ nexmo app:create "Application name" --type rtc --keyfile private.key
>Application created: xxxxxxxx-xxxx-xxxx-xxxx-xxxxsxxxxxxx
>Private Key saved to: 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]

Usage

Create an instance and login

var rtc = new ConversationClient({debug:false});
// var token = request login token as above, with sub=<username>
rtc.login(token).then(
    function(application){
        // use the application object to manage the conversations
        // access the available conversations
        console.log(application.conversations);
    });

Create a new Conversation

var conversationData = {
    display_name:'Nexmo Conversation'
    };
application.newConversation(conversationData).then(
    function(conversation) {
        // join the created conversation
        conversation.join().then(
            function(member) {
                console.log("Joined as " + member.user.name);
            });
    }).catch(function(error) {
    console.log(error);
});

Get a conversation you are a member of

application.getConversation(conversation_id).then(
    function(conversation) {
         // console.log(conversation);
        });

Set up Text listener for incoming Text Events

conversation.on("text", function(sender, textEvent){
    if (textEvent.cid === conversation.id){
        // if (rtc.isVisible) { textEvent.seen(); }
        console.log("message received:", 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);
    });

Login Create a conversation, join it, send a message and listen for incoming messages

var rtc = new ConversationClient({debug:false});
// var token = request login token as above, with sub=<username>
rtc.login(token).then(
    function(application){
        // access the available conversations
        console.log(application.conversations);
        // you might already have conversations under `application.conversations[]` to get,
        // or join (if application.conversations[xxx].me.state === "joined" || "invited" respectively)

        application.newConversation().then( //not providing a name, the service will assign a random one for you
            function(conversation) {
                // join the created conversation
                // you can also find your user_id under rtc.application.me (as application === rtc.application)
                conversation.join().then(
                    function(member) {
                        console.log("Joined as " + member.user.name); //member.id is your member's id
                    });

                // listen for incoming text messages
                conversation.on("text", function(sender, textEvent){
                        // manage seen indication
                        // if (rtc.isVisible) { textEvent.seen(); }
                        console.log("message received:", textEvent, sender);
                });

                // send a text event to the conversation
                conversation.sendText("Hi Nexmo").then(
                    function(){
                        console.log('message was sent');
                    }).catch(function(error){
                        console.log('error sending the message', error);
                });
            }).catch(function(error) {
            console.log(error);
        });
    });

The main objects here are application and conversation you can use them to extend the use case. E.g. by inviting a second user to a conversation that you are a member (have joined). The second instance should see the conversation listed, and the member(or me).state as "invited" When both instances have attached listener to "text" events of a conversation they have both joined, you should be able to send/receive text events in that conversation.

Build

If you want to contribute:

Install SDK

clone this repo and enter in it's directory

$ npm install

build

$ npm start

it will build interracially your code.

or

$ npm run build

generate docs

$ npm run docs

it will generate your code in dist/conversationClient

Run the tests

you can run the test with

$ npm run test

NEXMO

0.1.21

7 years ago

0.1.21-beta

7 years ago

0.1.20

7 years ago

0.1.20-beta.2

7 years ago

0.1.20-beta.1

7 years ago

0.1.20-beta

7 years ago

0.1.19

7 years ago

0.1.18

7 years ago

0.1.17

7 years ago

0.1.16

7 years ago

0.1.15

7 years ago

0.1.15-beta

7 years ago

0.1.14

7 years ago

0.1.13

7 years ago

0.1.14-beta

7 years ago

0.1.13-beta.3

7 years ago

0.1.13-beta

7 years ago

0.1.12

7 years ago

1.0.0

7 years ago

0.1.11

7 years ago

0.1.10

7 years ago

0.1.9

7 years ago

0.1.8

7 years ago

0.1.7

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.47

8 years ago

0.0.46

8 years ago

0.0.4-5.2

8 years ago

0.0.4-5.1

8 years ago

0.0.45

8 years ago

0.0.43

8 years ago

0.0.42

8 years ago

0.0.41

8 years ago

0.0.40

8 years ago