How to return value from method server to client

hi, I try to go by the given server to the client via a method, you would know tell me if the procedure is correct?

how can I make a decent return?

cordially

server.js

Meteor.methods({
    checkapi: function () {
	var autobahn = Meteor.npmRequire('autobahn');
	var wsuri = "wss://api.poloniex.com";
	var connection = new autobahn.Connection({
	  url: wsuri,
	  realm: "realm1"
	});
	connection.onopen = function (session) {
		function marketEvent (args,kwargs) {
			//console.log(args);
		}
		function tickerEvent (args,kwargs) {
			//console.log(args);
		}
		function trollboxEvent (args,kwargs) {
			//console.log(args);
		}
		session.subscribe('BTC_ETH', marketEvent);
		session.subscribe('ticker', tickerEvent);
		session.subscribe('trollbox', trollboxEvent);
	}
	 
	connection.onclose = function () {
	  console.log("Websocket connection closed");
	}
			       
	connection.open();
	

    }
});

client.js


Meteor.call("checkapi", "foo", function(error, results) {
    //console.log(results.content); //results.data should be a JSON object
if(error){
        console.log("error:"+error);
    } else {
	//console.log("results:"+results);
	Session.set('myMethodResult', results);
	
    }

});
  // use reactive Session variable in helper
  Template.livepoloniex.helpers({
    myHelper: function(){
      return Session.get('myMethodResult'); // "bar"
    }
  });

poloniex.html


Live POlo

{{myHelper}}

This is a really complex example.

The short answer is you can return in your server method and that return goes to the client. If you are doing anything async then you need to get it bound to a meteor environment. Look into wrap async on the meteor docs. It can help you there, but what it looks like is that you are making a second connection to another system, and have some complexity there.

In that case, what I would do is read up on the fibers API so that you can understand how to suspend and resume fibers, and at the end of your async operation, when you have your result you can save it in the closure and then resume the fiber and then you can return the result.

call/method is used to return a one-time result driven on request by the client. By setting up event notifications in autobahn you are subscribing to streamed events, so can never get a sensible one-time result.

I’ve just run (a variation of) your code and I see a solid wall of activity from all 3 event streams … so my question is what do you actually want to do?

2 Likes

I want to use the data collected with the client-side API but I can not find a reliable procedure

My feeling is that this would be best served by running the autobahn code on the client. Having said that, I’ve just tried it and the websocket is closed down immediately, so perhaps that’s not going to work.

The closest paradigm to what you want seems to be the MongoDB livequery, which observes changes on a database and mirrors those in minimongo via Meteor’s pub/sub. In terms of concept it’s not unlike that, but the rate of data transfer to multiple connected clients may be excessive without careful filtering. It would be like livequery on a very active database.

So, here’s a repo which delivers a subset (trollbox) of events in real time to the browser.

It uses 1.3-rc.8 of Meteor to import autobahn, but is otherwise “standard” Meteor.

git clone https://github.com/robfallows/trollbox.git trollbox
cd trollbox
meteor

Don’t leave this running on your browser for long - the client-side collection will keep on growing until your browser falls over.

thank you for your return, I will study your code to understand the journey and reproduce

thank you again for your help

1 Like

its me again ! :smiley:
you know tell me how to implement a proper procedure to add autobahn ?

simple “npm install autobahn” ?