client-341420_640Today I'll be writing about the easiest way of getting data from server to client on Meteor, data that is not necessarily DB related.

First lets start with the DB related data.
Usually if you want to get data from the server side to the client side on Meteor (assuming autopublish was removed of course) you'd use Publish and Subscribe:

1
2
3
4
5
6
7
8
//Publication
Meteor.publish( 'pomodoros', function() {
  return Pomodoros.find({ userId: this.userId });
});
//Subscription
Meteor.subscribe( 'pomodoros' );
//Client side jibber-jabber
Pomodoros.findOne({ userId: Meteor.userId() });

This example was taken from my Meteoro app, and it shows a simple way of getting Collection data from the server on the client side.

In the following scenario however, I was trying to get Stocks Quotes from Yahoo Finance - none DB related.
I found a meteor package called ajbarry:yahoo-finance, which is a simple wrapper for the node-yahoo-finance NPM package.
The example seemed pretty simple (I only care about snapshot for this project):

1
2
3
4
5
6
yahooFinance.snapshot({
  symbols: ['AAPL','GOOG']
  fields: ['s', 'n', 'd1', 'l1', 'y', 'r'],
}, function (err, snapshot) {
  //..
});

But I had a problem, for some reason the yahooFinance object wasn't defined on the client side.
Later I found out the object is only available on the server side, to expose it to the client, I had to use Methods:
server/methods.js

1
2
3
4
5
Meteor.methods({
  getQuote: function( stockname ) {
    return YahooFinance.snapshot({symbols: [stockname] });
  }
})

And now I was able to access it via the client:

1
2
3
4
5
6
7
8
9
10
11
12
Template.stocks.onRendered(function() {
  if ( _.isEmpty(Session.get('GOOG')) ) {
    Meteor.call('getQuote', 'GOOG', function(err, result) {
      Session.set('GOOG', result.GOOG);
    });
  }
})
Template.stocks.helpers({
  stock: function() {
    return Session.get('GOOG');
  }
})

Template:

1
2
3
4
5
6
7
8
<template name="stocks">
  <h2></h2>
  <ul>
    <li><strong>Name</strong> </li>
    <li><strong>Ask</strong> </li>
    <li><strong>Bid</strong> </li>
  </ul>
</template>

Result:

google-stock

Done!

Side Note:
There's a small problem with the package, the node-yahoo-finance version used (0.1.4) on the Meteor package is quite outdated, and lacks the single stock snapshot (which is why the single stock had to be wrapped with [] when calling the function).

I'm considering forking the package, will try to write about it here.