PubNub Twitter数据流设置不返回数据

时间:2017-02-07 09:30:09

标签: javascript api d3.js twitter pubnub

尝试使用D3在世界地图上映射推文,并首次使用PubNub获取实时数据流。设置时遇到问题并获得以下代码:

var margin = {top: 20, right: 20, bottom: 20, left: 20};

var w = 1100 - margin.left - margin.right,
    h = 900 - margin.top - margin.bottom;

var svg = d3.select("#chart")
            .append("svg")
              .attr("width", w + margin.left + margin.right)
              .attr("height", h + margin.top + margin.bottom)
            .append("g")
              .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var geoData = "https://raw.githubusercontent.com/johan/world.geo.json/master/countries.geo.json";

d3.json(geoData, function(data){

    var geo = data.features;

    var projection = d3.geo.mercator()
                       .scale(150)
                       .translate([w/2,h/2]);

    var path = d3.geo.path()
                     .projection(projection);

    svg.selectAll("path")
       .data(geo)
       .enter()
       .append("path")
       .attr("fill", "#95E1D3")
       .attr("stroke", "#34495e")
       .attr("stroke-width", 0.5)
       .attr("class", function(d){ return d.properties.name})
       .attr("d", path);

var pubnub = new PubNub({

     subscribeKey : "my key"

   });

    pubnub.subscribe({

    channels: ['pubnub-twitter'],

    withPresence: true

     });

   pubnub.addListener({

    message: function(m) {

        console.log(m);    

    },

   presence: function(p){

     console.log(p);

   },

   status: function(s){

     console.log(s);

   }

  })


})

我希望它将消息打印到控制台,但没有任何反应。使用PubNub v4。任何人都可以帮我吗?

这是一个有效的Codepen:

http://codepen.io/chemok78/full/mRjXzb/

1 个答案:

答案 0 :(得分:2)

Twitter样本Feed流式推文

以下示例将在console.log()中打印所有推文。您可以通过单击“运行代码段”按钮自行测试。您将在控制台中看到一组推文。

// Create PubNub Socket Handler
const pubnub = new PubNub({
    publishKey   : 'empty'
,   ssl          : true
,   subscribeKey : 'sub-c-78806dd4-42a6-11e4-aed8-02ee2ddab7fe'
});

// Subscribe to Twitter feed
console.log("Subscribing to Live Twitter Stream.");
pubnub.subscribe({ channels: ['pubnub-twitter'] });

// Add Socket Event Function Handlers
pubnub.addListener({
    status  : statusEvent => console.log(statusEvent)
,   message : message     => console.log(message)
});
<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.4.2.min.js"></script>

PubNub在2014年发布了一个博客,展示了how to analyze tweets from Twitter's Firehose

Twitter Tweet Feed Stream