hypno2000
hypno2000

Reputation: 61

forwarding server side DDP connection collections to client

I have backend meteor server which serves and shares common collections across multiple apps (just sharing mongo db is not enough, realtime updates are needed).

     BACKEND
     /     \
   APP1     APP2
     |       |
  CLIENT   CLIENT

I have server-to-server DDP connections running between backend server and app servers.

Atm i'm just re-publishing the collections in app server after subscribing them from backend server.

It all seems working quite well. The only problem tho is that in app server cant query any collections in server side, all the find() responses are empty, in client side (browser) it all works fine tho.

Is it just a coincidence that it works at all or what do you suggest how i should set it up.

Thanks

Upvotes: 1

Views: 669

Answers (2)

nicolsondsouza
nicolsondsouza

Reputation: 426


I have done this already,
check my app Tapmate or youtap.meteor.com on android and iphone,
I know it will work till 0.6.4 meteor version,
haven't checked if that works on above version,

You have to manually override the default ddp url while connecting,
i.e. go to live-data package in .meteor/packages/live-data/stream_client_socket.js

overwrite this - Meteor._DdpClientStream = function (url) {
url = "ddp+sockjs://ddp--**-youtap.meteor.com/sockjs";
now you won't see things happening locally but it will point to meteor server
also disable reload js from reloading

Thanks

Upvotes: 0

fletch
fletch

Reputation: 1641

I realize that this is a pretty old question, but I thought I would share my solution. I had a similar problem as I have two applications (App1 and App2) that will be sharing data with a third application (App3).

I couldn't figure out why the server-side of my App1 could not see the shared collections in App3...even though the client-side of App1 was seeing them. Then it hit me that the server-side of my App1 was acting like a "client" of App3, so needed to subscribe to the publication, too.

I moved my DDP.connection.subscribe() call outside the client folder of App1, so that it would be shared between the client and server of App1. Then, I used a Meteor.setInterval() call to wait for the subscription to be ready on the server side in order to use it. That seemed to do the trick.

Here's a quick example:

in lib/common.js:

Meteor.myRemoteConnection = DDP.connect(url_to_App3);
SharedWidgets = new Meteor.Collection('widgets', Meteor.myRemoteConnection);
Meteor.sharedWidgetsSubscription = Meteor.myRemoteConnection.subscribe('allWidgets');

in server/fixtures.js:

Meteor.startup(function() {
    // check once every second to see if the subscription is ready
    var subIsReadyInterval = Meteor.setInterval(function () { 
        if ( Meteor.sharedWidgetsSubscription.ready() ) {
            // SharedWidgets should be available now...
            console.log('widget count:' + SharedWidgets.find().count);
            // clean up the interval...
            Meteor.clearInterval(subIsReadyInterval);
        }
    }, 1000);
});

If there is a better way to set this up, I'd love to know.

Upvotes: 2

Related Questions