A
A
Alexander Wolf2015-05-26 16:22:00
Node.js
Alexander Wolf, 2015-05-26 16:22:00

Asynchronous code in Meteor server?

Hey! Started making the first application on Meteor. I immediately decided to go beyond the boundaries of ordinary business card sites.
On the server side, there is some kind of daemon - a code that must constantly spin and do something (for example, receive messages from the VK API through long polling).
When I try to make an HTTP.post, I get an error saying Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.
Googled it, didn't find anything useful (and yes, I've seen SO).
Tell me where to read about asynchronous code on the server or the right way to write something like that.

this.World = function() {
  this.vk = new VK({ appId: 777, mode: 'oauth' });
  this.vk.token = '#token';

  this.viewFriends(5);
  this.listenMessages();
};

this.World.prototype = {
  viewFriends: function(time) {
    var vk = this.vk;

    setInterval(function() {
      var list = vk.request('friends.getRequests', { extended: 0 }, function(data) {
        data.response.forEach(function(id) {
          vk.request('friends.add', { user_id: id }, function() {
            vk.request('messages.send', {
              user_id: id,
              message: 'Привет, новый друг!'
            }, function(res) {
              console.log(res);
            })
          });
        });
      })
    }, 1000 * 60 * time);
  },
  listenMessages: function() {
    var vk = this.vk;

    vk.request('messages.getLongPollServer', {}, function (data) {
      data = data.response;
      var url = ['http://', data.server, '?act=a_check&key=', data.key, '&ts=', data.ts, '&wait=25&mode=2']

      HTTP.post(url.join(''), Meteor.bindEnvironment(function(err, data) {
        console.log(123);
      }));
    });
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Wolf, 2015-06-05
@mannaro

Started using node-sync lib.

A
achirkof, 2015-11-02
@achirkof

Well told here .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question