Embedding twitter posts using publish-subscribe (no autopublish)


#1

Hi guys,

I’m trying to embed tweets, using the twitter api. So far, so good. I’ve got the managed to get the embed codes from twitter, and i’ve managed to successfully embed them using autopublish,
But now, I want to to the same thing, using publish subscribe. I’ve written my methods, but apparently it does’n work.
I put all my embed codes in one array and publish it once because I had duplication problems it I added them one by one.

Here’s my code:

server.js

Meteor.methods({
'twitterSearch': function(term) {
try {
  var connections = {}
  var html_embed = []
  var id
  var array = twitter.search(term)
  var content = JSON.parse(array['content'])
  content = content['statuses']

  for (var i = 0; i < content.length; i++) {
    id = content[i]['id_str']
    var rawEmbed = twitter.callAsApp('GET', 'statuses/oembed.json', {
      id: id
    })
    rawEmbed = JSON.parse(rawEmbed['content'])

    //putting all embed codes in one big array
    html_embed.push({html: rawEmbed['html']})
  }

  //and now I'm publishing it
  Meteor.publish('tweetPublication', function() {
    var init = true
    var subscription = this

    connections[subscription._session.id] = subscription

    subscription.added('tweets', Random.id(), tweet)

    subscription.onStop(function(){
      delete connections[subscription._session.id]
    })
  })
} catch (e) {
  console.log(e)
  return null
}
})

client.js

Tweets = new Mongo.Collection('tweets')
Meteor.subscribe('tweetPublication')

tweets.html:

<template name="tweet">
{{#each data}}
  {{> html_embed}}
{{/each}}
{# <li>{{{html}}}</li> #}
</template>

<template name="html_embed">
   {{{html}}}
</template>

main.html:

<h4>Tweets!!!</h4>
<ul>
{{#each tweets}}
  {{> tweet}}
{{/each}}