-1

I'm using Ably to implement Pub/Sub in my application but I'm getting no errors but at the same time I'm not receiving any published messages. I'm not sure what's wrong. Help appreciated.

Here's my code:

//publisher
var Ably = require('ably');
var apiKey = '';
var ably = new Ably.Realtime({key: apiKey});
var pubChannel = ably.channels.get("sports");
setInterval(function(){
    pubChannel.publish('update', {'Team': 'Man United'})
},1000)

//subscriber
var apiKey = '';
var ably = new Ably.Realtime({key: apiKey});
var subChannel = ably.channels.get("Sports");
subChannel.subscribe(function(msg){
    document.getElementById('text').innerHTML = JSON.stringify(msg.data);
})

(disclaimer: I am a developer advocate for Ably, and posting and self-answering a commonly asked support question here on Stack Overflow so our users can find this more easily)

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • While it's commendable to want to write a self-answered question to share info with others, the question still has to meet the same quality standards as any other question here. This one does not. If someone other than you posted it, it would be closed as too broad. Please [edit] to improve the quality of the question. (You're going to have issues with all of the off-site links you've provided in your answer as well, BTW; you may want to deal with those now as well.) – Ken White Apr 05 '18 at 16:36
  • 1
    Thanks for the feedback, I'll edit the question @KenWhite – Srushtika Neelakantam Apr 05 '18 at 16:41

1 Answers1

1

Here's what I found as a general debugging process:

First, make sure the channel you're publishing on is the same channel you're subscribing to. (Ably channel names are case-sensitive -- sports is a different channel to Sports).

Second, make sure you're using the same app everywhere (make sure you're not, say, publishing with an api key from your Sandbox app, but subscribing with an api key from your Production app)

Thirdly, you need to find out whether the problem is on the publishing or the subscribing side. Open the dev console, attach to the channel that you're publishing on, and try a publish. Do you see the message appear there (but still not on your subscribing device)? If yes, the problem is on the subscribing side. If no, the problem's on the publishing side.

Debugging publish problems

When you call channel#publish, you can pass a callback (or equivalent - select the language you're using from the language bar), which if the publish failed will tell you why.

Debugging subscribe problems