OscarVGG
OscarVGG

Reputation: 2670

How to create a Twilio channel from node js

I'm coding an app that must create a chat channel when some conditions are fulfilled after the user updates a database table (I need to create a chat channel from the server side).

I'm using Node.js from AWS Lambda and twilio-chat. But I'm unable to create the client. Here is my code:

const Twilio = require('twilio-chat');

var chatClient = Twilio.Chat.Client.create(token)

I'm getting the following error: Cannot read property 'Client' of undefined

What I'm I doing wrong?

Upvotes: 0

Views: 550

Answers (1)

philnash
philnash

Reputation: 73057

Twilio developer evangelist here.

The twilio-chat module is for use in the client side and uses browser websockets to connect to the Twilio Chat service. It is not build for server side use.

To create a channel from the server side you should use the Twilio Node.js module and the Twilio Chat REST API. You can create a channel like this:

var accountSid = 'your_account_sid';
var authToken = 'your_auth_token';
var serviceSid = 'your_chat_service_sid';

var Twilio = require('twilio').Twilio;

var client = new Twilio(accountSid, authToken);
var service = client.chat.services(serviceSid);

service.channels.create({
    friendlyName: 'MyChannel'
}).then(function(response) {
    console.log(response);
}).catch(function(error) {
    console.error(error);
});

Let me know if this helps at all.

Upvotes: 2

Related Questions