[英]meteor.js - temporary server-side app state
I need to use some data from a 3rd party API in my app, poll for the needed data with certain frequency from the server, and make it available to the client. 我需要在我的应用程序中使用来自第三方API的一些数据,以一定频率从服务器轮询所需的数据,并将其提供给客户端。 The easiest way would be to create a collection and update it, and make the data available to the client via pub/sub.
最简单的方法是创建一个集合并对其进行更新,然后通过pub / sub将数据提供给客户端。 But, in this particular case I don't need to store that data or keep track of it, and it updates very frequently, so storing it to db would actually be just additional unneeded work.
但是,在这种特殊情况下,我不需要存储或跟踪数据,并且数据会非常频繁地更新,因此将其存储到db实际上只是其他不必要的工作。 I would prefer to store it somehow in the RAM, and make it available to the client in some other way except collections (perhaps, return from a method call).
我宁愿以某种方式将其存储在RAM中,并通过除集合(也许是从方法调用返回)以外的其他方式将其提供给客户端。 But I'm not sure, how to do that.
但是我不确定如何做到这一点。 Could someone suggest some nice approach?
有人可以建议一些好的方法吗?
You could use this package meteor-publish-join to fetch data from external API and publish to client periodically (disclaimer: I am the author): 您可以使用此包meteor-publish-join从外部API获取数据并定期发布到客户端(免责声明:我是作者):
Server: 服务器:
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));
},
});
});
Client: 客户:
import { JoinClient } from 'meteor-publish-join'; 从'meteor-publish-join'导入{JoinClient};
Meteor.subscribe('test');
// Get the values published within `test` publication. All these values are reactive
JoinClient.get('withPromise')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.