Skip to main content
Question

PubNub Subscription Event not detecting call notification

  • November 16, 2021
  • 13 replies
  • 697 views

Running in node.js .

Not picking up on any call action after successful subscription.

My Code:


  1. function setSubscription() {
  2. const platform = rcsdk.platform()
  3. platform.login({
  4. username: '+14706150273',
  5. extension: '101',
  6. password: '********'
  7. }).then(response => {
  8. const subscription = rcsdk.createSubscription().setEventFilters(['/account/~/telephony/sessions']);
  9. subscription.on(subscription.events.notification, function (msg) {
  10. console.log(msg);
  11. });
  12. subscription.register().then(function (response) {
  13. console.log(response.json());
  14. console.log('Success: Subscription is listening');
  15. }).catch(function (e) {
  16. console.log('Subscription Error: ' + e.message);
  17. });
  18. }).catch(e => {
  19. console.error(e)
  20. })
  21. }


Console output after run showing successfully subscribed (but nothing after) - Calls are showing in the sandbox call log:

  1. [nodemon] starting `node index.js`
  2. Server is running on port: 5000
  3. {
  4. uri: 'https://platform.devtest.ringcentral.com/restapi/v1.0/subscription/edde4bf6-1563-4263-94c7-247954e3ac68',
  5. id: 'edde4bf6-1563-4263-94c7-247954e3ac68',
  6. creationTime: '2021-11-16T22:52:03.946Z',
  7. status: 'Active',
  8. eventFilters: [ '/restapi/v1.0/account/307128004/telephony/sessions' ],
  9. expirationTime: '2021-11-16T23:07:03.946Z',
  10. expiresIn: 899,
  11. deliveryMode: {
  12. transportType: 'PubNub',
  13. encryption: false,
  14. address: '4174641560134678_4a93035d',
  15. subscriberKey: 'sub-c-b8b9cd8c-e906-11e2-b383-02ee2ddab7fe'
  16. }
  17. }
  18. Success: Subscription is listening

13 replies

PhongVu
Community Manager
Forum|alt.badge.img
  • Community Manager
  • November 17, 2021

Can you add these 2 functions and call the readAllSubsciptions() to see if you have multiple subscriptions. If there is more than one, delete them and retry subscribe for a new subscription.

function deleteRegisteredSubscription(id) {
  platform.delete('/subscription/' + id)
    .then(function (response){
        console.log(response)
    })
    .catch(function(e){
      console.log(e.message)
    });
}

function readAllSubscriptions() {
  platform.get('/subscription')
    .then(function (response) {
      var json = response.json()
      if (json.records.length > 0){
        for (var record of json.records) {
          console.log(record)
        }
      }else{
        console.log("No subscription")
      }
    })
    .catch(function(e) {
      console.log(e.message)
    });
}

  • Author
  • Known Participant
  • November 17, 2021

This initially worked - checking that only 1 subscription is active. However, running this again (Same code exactly), yields the same non responsive result.


Any insight?


  • Author
  • Known Participant
  • November 17, 2021
  1. const rcsdk = new SDK({
  2. server: rcCred.RINGCENTRAL_SERVER,
  3. appKey: rcCred.RINGCENTRAL_CLIENTID,
  4. appSecret: rcCred.RINGCENTRAL_CLIENTSECRET,
  5. });
  6. const platform = rcsdk.platform();
  7.  
  8. rcsdk.platform()
  9. .login({
  10. username: rcCred.RINGCENTRAL_USERNAME, // phone number in full format
  11. extension: '', // leave blank if direct number is used
  12. password: rcCred.RINGCENTRAL_PASSWORD
  13. })
  14. .then(async function (response) {
  15. const authData = platform.auth().data();
  16. platform.auth().setData(authData
  17. console.log('logged in
  18. })


Using these exact credentials with the other SDK and it works (the auth)


PhongVu
Community Manager
Forum|alt.badge.img
  • Community Manager
  • November 17, 2021

Why do you need this right after authenticated?

const authData = platform.auth().data();
platform.auth().setData(authData ...

Anyway, I masked the sensitive info you post in your previous comment. I could login with my code and here is what you should implement the authentication

const RingCentral = require('@ringcentral/sdk').SDK
const Subscriptions = require('@ringcentral/subscriptions').Subscriptions;
const rcsdk = new RingCentral({
  server: RINGCENTRAL_SERVER,
  clientId: RINGCENTRAL_CLIENTID,
  clientSecret: RINGCENTRAL_CLIENTSECRET
})

var platform = rcsdk.platform();
const subscriptions = new Subscriptions({
   sdk: rcsdk
});
var subscription = subscriptions.createSubscription();
platform.login({
        username: '...',
        extension: '',
        password: '...'
      })

platform.on(platform.events.loginSuccess, function(e){
    console.log("Login success")
    subscribe_for_telephony_notification()
    
});
async function subscribe_for_telephony_notification(){
  var eventFilters = [
    '/restapi/v1.0/account/~/telephony/sessions'
  ]
  subscription.setEventFilters(eventFilters)
  .register()
  .then(async function(subscriptionResponse) {
      console.log("Ready to receive Tel session events via PubNub.")
  })
  .catch(function(e) {
    console.error(e.message);
  })
}
subscription.on(subscription.events.notification, function(msg) {
    console.log(JSON.stringify(msg));
    console.log("======");
});

  • Author
  • Known Participant
  • November 17, 2021

Thanks the auth works.

Now, I copied your code exactly - running the readAllSubscriptions returns a 404 error, and no activity is logged.


PhongVu
Community Manager
Forum|alt.badge.img
  • Community Manager
  • November 18, 2021

Here are the events

{"uuid":"8358091234666434757","event":"/restapi/v1.0/account/307128004/telephony/sessions","timestamp":"2021-11-18T18:24:01.439Z","subscriptionId":"b948c2ad-65f5-408b-a5a4-ebff3ddedb89","ownerId":"307128004","body":{"sequence":2,"sessionId":"13964140005","telephonySessionId":"s-35d3846eadf9471e8dd8edc3b23014c6","serverId":"10.29.20.87.TAM","eventTime":"2021-11-18T18:24:01.119Z","parties":[{"accountId":"307128004","id":"p-35d3846eadf9471e8dd8edc3b23014c6-1","direction":"Outbound","to":{"phoneNumber":"+1470615xxxx"},"from":{"phoneNumber":"+1210306xxxx"},"status":{"code":"Setup","rcc":false},"park":{},"missedCall":false,"standAlone":false,"muted":false}],"origin":{"type":"Call"}}}
======
{"uuid":"6932542151911887543","event":"/restapi/v1.0/account/307128004/telephony/sessions","timestamp":"2021-11-18T18:24:01.447Z","subscriptionId":"b948c2ad-65f5-408b-a5a4-ebff3ddedb89","ownerId":"307128004","body":{"sequence":3,"sessionId":"13964140005","telephonySessionId":"s-35d3846eadf9471e8dd8edc3b23014c6","serverId":"10.29.20.87.TAM","eventTime":"2021-11-18T18:24:01.146Z","parties":[{"accountId":"307128004","id":"p-35d3846eadf9471e8dd8edc3b23014c6-2","direction":"Inbound","to":{"phoneNumber":"+1470615xxxx","name":"Manager Desk","extensionId":"307128004"},"from":{"phoneNumber":"+1210306xxxx"},"status":{"code":"Setup","rcc":false},"park":{},"missedCall":false,"standAlone":false,"muted":false}],"origin":{"type":"Call"}}}
======
{"uuid":"8858268169076275026","event":"/restapi/v1.0/account/307128004/telephony/sessions","timestamp":"2021-11-18T18:24:01.544Z","subscriptionId":"b948c2ad-65f5-408b-a5a4-ebff3ddedb89","ownerId":"307128004","body":{"sequence":4,"sessionId":"13964140005","telephonySessionId":"s-35d3846eadf9471e8dd8edc3b23014c6","serverId":"10.29.20.87.TAM","eventTime":"2021-11-18T18:24:01.444Z","parties":[{"accountId":"307128004","id":"p-35d3846eadf9471e8dd8edc3b23014c6-1","direction":"Outbound","to":{"phoneNumber":"+1470615xxxx","name":"Manager Desk","extensionId":"307128004"},"from":{"phoneNumber":"+1210306xxxx"},"status":{"code":"Answered","rcc":false},"park":{},"missedCall":false,"standAlone":false,"muted":false}],"origin":{"type":"Call"}}}
======
{"uuid":"4294381325951201884","event":"/restapi/v1.0/account/307128004/telephony/sessions","timestamp":"2021-11-18T18:24:04.960Z","subscriptionId":"b948c2ad-65f5-408b-a5a4-ebff3ddedb89","ownerId":"307128004","body":{"sequence":6,"sessionId":"13964140005","telephonySessionId":"s-35d3846eadf9471e8dd8edc3b23014c6","serverId":"10.29.20.87.TAM","eventTime":"2021-11-18T18:24:04.837Z","parties":[{"accountId":"307128004","id":"p-35d3846eadf9471e8dd8edc3b23014c6-2","direction":"Inbound","to":{"phoneNumber":"+1470615xxxx","name":"Manager Desk","extensionId":"307128004"},"from":{"phoneNumber":"+1210306xxxx"},"status":{"code":"Disconnected","reason":"CallerDropped","rcc":false},"park":{},"missedCall":false,"standAlone":false,"muted":false}],"origin":{"type":"Call"}}}
======
{"uuid":"4150499124955922640","event":"/restapi/v1.0/account/307128004/telephony/sessions","timestamp":"2021-11-18T18:24:04.951Z","subscriptionId":"b948c2ad-65f5-408b-a5a4-ebff3ddedb89","ownerId":"307128004","body":{"sequence":5,"sessionId":"13964140005","telephonySessionId":"s-35d3846eadf9471e8dd8edc3b23014c6","serverId":"10.29.20.87.TAM","eventTime":"2021-11-18T18:24:04.833Z","parties":[{"accountId":"307128004","id":"p-35d3846eadf9471e8dd8edc3b23014c6-1","direction":"Outbound","to":{"phoneNumber":"+1470615xxxx","name":"Manager Desk","extensionId":"307128004"},"from":{"phoneNumber":"+1210306xxxx"},"status":{"code":"Disconnected","rcc":false},"park":{},"missedCall":false,"standAlone":false,"muted":false}],"origin":{"type":"Call"}}}

  • Author
  • Known Participant
  • November 18, 2021

I'm not sure what the issue is. Would you mind sending the entire code you have? I even tried a new clean slate server, with nothing but the code I posted above and still getting 404 with the new sdk.


Thanks for the effort.


PhongVu
Community Manager
Forum|alt.badge.img
  • Community Manager
  • November 18, 2021

Here is the entire code. Just add the app client secret and provide your username and password

const RingCentral = require('@ringcentral/sdk').SDK
const Subscriptions = require('@ringcentral/subscriptions').Subscriptions;

RINGCENTRAL_CLIENTID = '92pPUVd4S1GMn5H07641rg'
RINGCENTRAL_CLIENTSECRET = 'client-secret'
RINGCENTRAL_SERVER = 'https://platform.devtest.ringcentral.com'
RINGCENTRAL_USERNAME = '1470615xxxx'
RINGCENTRAL_PASSWORD = 'password'
RINGCENTRAL_EXTENSION = ''

const rcsdk = new RingCentral({
  server: RINGCENTRAL_SERVER,
  clientId: RINGCENTRAL_CLIENTID,
  clientSecret: RINGCENTRAL_CLIENTSECRET
})

var platform = rcsdk.platform();
platform.login({
        username: RINGCENTRAL_USERNAME,
        extension: RINGCENTRAL_EXTENSION,
        password: RINGCENTRAL_PASSWORD
      })

platform.on(platform.events.loginSuccess, async function(e){
    console.log("Login success")
    await subscribe_for_telephony_notification()
    // if checking subscription, comment the previous line and uncomment the next line. 
    //await readAllSubscriptions()
});

const subscriptions = new Subscriptions({
   sdk: rcsdk
});
var subscription = subscriptions.createSubscription();

async function subscribe_for_telephony_notification(){
  var eventFilters = [
    '/restapi/v1.0/account/~/telephony/sessions'
  ]
  subscription.setEventFilters(eventFilters)
  .register()
  .then(async function(subscriptionResponse) {
      console.log("Ready to receive Tel session events via PubNub.")
  })
  .catch(function(e) {
    console.error(e.message);
  })
}

async function deleteRegisteredSubscription(id) {
  try{
    var resp = await platform.delete('/restapi/v1.0/subscription/' + id)
    console.log(resp)
  }catch(e) {
      console.log(e.message)
  }
}

async function readAllSubscriptions() {
  try {
    var resp = await platform.get('/restapi/v1.0/subscription')
    var jsonObj = await resp.json()
    if (jsonObj.records.length > 0){
      for (var record of jsonObj.records) {
        if (record.deliveryMode.transportType == 'PubNub'){
          console.log(record)
          // uncomment the next line of need to delete a subscription
          await deleteRegisteredSubscription(record.id)
        }
      }
    }else{
      console.log("No subscription")
    }
  }catch(e) {
    console.log(e.message)
  }
}

subscription.on(subscription.events.notification, function(msg) {
    console.log(JSON.stringify(msg));
    console.log("======");
});

  • Author
  • Known Participant
  • November 18, 2021

After digging in - the issue with reading the subscriptions was the end point that you provided in the beginning:

platform.get('/subscription')

In you're full code

/restapi/v1.0/subscription

So that's fixed, but we are back to the first issue - only one subscription is active, and i'm still not getting any notifications. Seems like the delete isn't removing something and it needs to wait to expire - if I wait for expiration it seems that the subscription does work.


Any insight on this?


  • Author
  • Known Participant
  • November 19, 2021

Eta on a fix?


PhongVu
Community Manager
Forum|alt.badge.img
  • Community Manager
  • November 23, 2021

The issue is fixed and PubNub notifications should work normal now. Please try again and report if you still face the problem.


  • Author
  • Known Participant
  • November 23, 2021

Verified - works.

Much appreciated your time and effort.


  • New Participant
  • November 23, 2021

fantastic thanks Phong Vu!