2017-02-20 36 views
0

我需要在我的应用程序中使用来自第三方API的一些数据,从服务器轮询一定频率的所需数据,并将其提供给客户端。最简单的方法是创建一个集合并更新它,并通过pub/sub将数据提供给客户端。但是,在这种特殊情况下,我不需要存储该数据或对其进行跟踪,并且它会更新频繁,因此将其存储到数据库实际上只是额外的不必要的工作。我宁愿将它以某种方式存储在RAM中,并以除集合外的其他方式(可能是从方法调用返回)将其提供给客户端。但我不确定,该怎么做。有人可以提出一些好方法吗?meteor.js - 临时服务器端应用程序状态

+0

将数据存储在内存中将通过声明一个变量,即'var someData = fetchedData'完成。 –

+0

你为什么害怕流星法? – ghybs

回答

1

你可以使用这个包meteor-publish-join来从外部API数据并发布到客户端定期(免责声明:我是作者):

服务器:

import { JoinServer } from 'meteor-publish-join'; 

Meteor.publish('test', function() { 

    // Publish a random value from an external API, plays well with promise, re-run every 10 seconds 
    JoinServer.publish({ 
    context: this, 
    name: 'withPromise', 
    interval: 10000, 
    doJoin() { 
     const id = parseInt(Math.random() * 100, 10); 

     return fetch(`https://jsonplaceholder.typicode.com/posts/${id}`) 
     .then(res => res.json()) 
     .then(data => data.title) 
     .catch(err => console.error(err)); 
    }, 
    }); 
}); 

客户:

进口来自'meteor-publish-join'的{JoinClient};

Meteor.subscribe('test'); 

// Get the values published within `test` publication. All these values are reactive 
JoinClient.get('withPromise') 
+0

这是一个很好的包,我已经看到它用于你的用例。 – DoctorPangloss