I want to use meteor publish composite two perform some join operations. I have two meteor application connected using ddp service. In which on first server i have given my code for publish composite. But how to call it from the server2.
My server1 code.
Meteor.publishComposite('board', function (userid) {
console.log('here')
return {
find: function () {
console.log(23456)
return Meteor.users.find(userid);
},
children: [{
find: function (user) {
return Apartment.find(user.lineup);
}]
}
});
Where to call Meteor.subscribe('boards');?
How can i use it from the other application which i connected using ddp service. Thanks in advance.
Here is how:
var otherServer = DDP.connect(url);
otherServer.subscribe(); // subscribe to collections
otherServer.call(); // call meteor methods
full list of methods at: http://docs.meteor.com/#/full/ddp_connect
Related
I am trying to build an Admin panel that will connect to another apps database and have the ability to change the data stored there.
var remote = DDP.connect('http://localhost:3000/');
var ServerAItems = new Mongo.Collection('items', { connection: remote });
Meteor.startup(function() {
console.log(remote);
remote.subscribe('smallBatchProducts', function(item){
});
console.log(ServerAItems.find().count(), 'test');
});
ServerAItems.find().count(); //returns 0
I have looked at Meteor: No docs from remote collection in template helper when connecting two apps via DDP and Connect two Meteor applications using DDP , but still can't figure out how to interact with the data and give the client access to it. The publication on localhost:3000 is smallBatchProducts.
I'm planning to have Flow Router handle the routing. Thanks
You should probably just put your console.log into the onReady callback.
Meteor.startup(function() {
remote.subscribe('smallBatchProducts', {
onReady: function(){
console.log(ServerAItems.find().count(), 'test');
},
onError: function(err) {}
});
});
Is it possible to preserve pointers (object references) when you send data from the server to the client with Meteor? My initial tests say No, but perhaps there is another way to do this.
Here are server-side and client-side scripts:
Server.js
Meteor.methods({
test: function test() {
var object = {
key: ['value']
}
var output = {
object: object
, reference: object.key
}
console.log(output.reference[0])
output.object.key[0] = "changed"
console.log(output.reference[0])
return output
}
})
Server output:
// value
// changed
Client.js
Meteor.call("test", testCallback)
function testCallback(error, data) {
if (!error) {
console.log(data.reference)
data.object.key[0]= "edited"
console.log(data.reference)
}
}
Client output in the console:
// changed
// changed
No, this is not possible, although subscriptions do something similar. If your data is not in a collection, you can still publish it (see this.added and friends in the Meteor docs), and the data will show up in a collection in the client.
I have async function on server side, that invoke from client and get data from some external sources by HTTP.call method. This is temporary data and I don't want put it in Mongo. Is a way to send this data to client except put in Mongo and do Meteor.publish?
This is piece of code:
Meteor.methods({
doRequest: function (partNumber) {
check(partNumber, String);
for (var i = 0; i < sources.length; i++) {
sources[i].params.nr = partNumber;
HTTP.call("POST", sources[i].url, { auth: sources[i].auth, params: sources[i].params }, requestHandler);
}
function requestHandler(err, res) {
if (err) {
throw new Meteor.Error("not-response", "Remote server not responding");
}
// need send array of objects to client
}
}
});
I see two ways to get this done:
Use a client only collection. You can actually publish any data not only collection cursors. Check this out: http://meteorcapture.com/publishing-anything/
[DEPRECATED] Use Meteor streams: http://arunoda.github.io/meteor-streams/
I have a public client side function:
test = function(){
alert("HELLO")!;
}
What I need is a function that works like this - so on the admin client script, they hit a button which calls a server method:
Meteor.call("callFunctions");
The server side function:
Meteor.methods({
callFunctions: function() {
//call clientside function test() for all clients
}
});
How can I do this without depending on a database?
My client-call package does that. It depends on database internally, but you don't have to worry about that dependence.
To call the methods on all clients you'll need to manually register and loop through their ids.
Set up method:
Meteor.ClientCall.methods({
'alert': function() {
...
},
});
Call it:
Meteor.ClientCall.apply(clientId, 'alert', []);
On the introducing article of DDP, I read that anything can be published, but I've read somewhere ( for example in this Stackoverflow comment Publish arbitrary data and automatically update HTML ) that only Collections can be published.
So where is the truth? If we can publish other things than Collections, I would liek to see an example as I can't find one so far.
From the docs: http://docs.meteor.com/#meteor_publish
Publish functions can return a Collection.Cursor, in which case Meteor will publish that cursor's documents. You can also return an array of Collection.Cursors, in which case Meteor will publish all of the cursors.
So at the moment you can only return a Collection via a cursor (result of a Collection.find()).
To return other data you need to hack into the sockjs stream (the socket library meteor uses to communicate to the server). Be aware this does not guarantee compatibility with future versions of meteor. Sockjs is the library used for meteor to communicate between the server (the wire)
from Publish arbitrary data and automatically update HTML*
client side js
sc = new Meteor._Stream('/sockjs');
sc.on('message', function(payload) {
var msg = JSON.parse(payload);
Session.set('a_random_message', JSON.stringify(msg.data));
});
Template.hello.greeting = function () {
return Session.get('a_random_message');
};
server side js
ss = new Meteor._StreamServer();
ss.register(function (socket) {
var data = {socket: socket.id, connected: new Date()}
var msg = {msg: 'data', data: data};
// Send message to all sockets (which will be set in the Session a_random_message of the client
_.each(ss.all_sockets(), function(socket) {
socket.send(JSON.stringify(msg));
});
});
You can also look at Meteor Streams too. See below.
assume you have added meteor streams via atmosphere - mrt add streams
sc = new Meteor.Stream('hello');
if(Meteor.isServer) {
Meteor.setInterval(function() {
sc.emit('a_random_message', 'Random Message: ' + Random.id());
}, 2000);
Meteor.permissions.read(function() { return true });
}
if(Meteor.isClient) {
sc.on('a_random_message', function(message) {
Session.set('a_random_message', message);
});
Template.hello.greeting = function () {
return Session.get('a_random_message');
};
}