Hi everybody!
I’m working with meteor and react.
I’m building a real-time trend chart.
The real time chart should shows the lasts 20 minutes, and must keep moving forward with the actual time.
I have imagined that the solution to this problem should be something like update the subscription parameters each second, maybe through a setInterval:
pseudo-code:
setInterval(
Meteor.subscribe('temperature', {gte: new Date()-20minutes, lt: new Date()}),
1000
)
output @12:00 (from 11:40 to 12:00):
> {_id: ..., date: 2018-05-24 12:00:00, temperature: 20.0}
{_id: ..., date: 2018-05-24 11:55:00, temperature: 20.0}
{_id: ..., date: 2018-05-24 11:50:00, temperature: 21.0}
{_id: ..., date: 2018-05-24 11:45:00, temperature: 23.0}
Another solution I’ve worked out is to subscribe to the last inserted document and pass its ‘date’ as upper-bound to the subscription (through an HOC):
class ChartComp extends Component {
...
}
ChartCompContainer = withTracker((props) => {
var gte = props.upperBound-20mins
var lt = props.upperBound
Meteor.subscribe('temperature', {gte: gte, lt: lt})
return {
temps: Temperature.find({}, {sort: {date: -1}}).fetch()
}
}
})(ChartComp)
export default ChartCompContainerContainer = withTracker((props) => {
Meteor.subscribe('lastTemperatureInserted')
return {
upperBound: Temperature.findOne({}, {sort: {date: -1}}).fetch()
}
}
})(ChartCompContainer)
in this way, as soon as a new document will be inserted, a new upperBound value is sent through props and the temperature subscription parameters updates to the last document inserted.
Can you find out a better solution? Which one is the best between the previous two?
Thankyou for your help!