meteorhacks / cluster

Clustering solution for Meteor with load balancing and service discovery
https://meteorhacks.com/cluster-a-different-kind-of-load-balancer-for-meteor.html
MIT License
631 stars 80 forks source link

cluster and cordova integration #27

Open banglashi opened 9 years ago

banglashi commented 9 years ago

Hi @arunoda, first of all, thanks for all the great work you are doing for the meteor community ;)

We are trying to build an app and would like to use the cluster package for service-discovery.

As soon as we run the app in the ios simulator or device we get following error:

Uncaught Error: SyntaxError: JSON Parse error: Unrecognized token '<':2087:http://meteor.local/packages/ddp.js?41b62dcceb3ce0de6ca79c6aed088cccde6a44d8

Basically the ddp call returns the raw html.

I have created a small demo project here https://github.com/banglashi/meteor-cordova-cluster to reproduce the issue. Just edit the CLUSTER_DISCOVERY_URL in the dev and mobile shell scripts. Than lunch the ./dev script for the search service and the ./mobile script for the web service.

It could be possible that I am doing/setting something wrong. Could it be related to the endpoint being localhost?

Any hint or suggestions is really appreciated.

Best regards

boustanihani commented 9 years ago

It's good to know if cluster supports cordova in its current state :+1:

arunoda commented 9 years ago

@banglashi not sure what was the issue is. I am having hard time setting up the cordova dev stuff. Let me try again.

banglashi commented 9 years ago

Hi @arunoda, The DDP call is actually being made to this base_url: "http://meteor.local/search/sockjs" Any idea why it's not using the correct endpoint url? Or is this correct like that? I am trying to read into it and understand the internals, will let you know if I find something more.

arunoda commented 9 years ago

Thanks. I'm not much familiar with this modulus. May be we can get some help from slava.

On Wed, Mar 18, 2015 at 8:15 PM banglashi notifications@github.com wrote:

Hi @arunoda https://github.com/arunoda, The DDP call is actually being made to this base_url: " http://meteor.local/search/sockjs" Any idea why it's not using the correct endpoint url? Or is this correct like that? I am trying to read into it and understand the internals, will let you know if I find something more.

— Reply to this email directly or view it on GitHub https://github.com/meteorhacks/cluster/issues/27#issuecomment-83003659.

fishdude commented 9 years ago

Hey @arunoda,

Thanks for all your terrific work!

Was anyone able to figure out a solution to this? When attempting to run on both iOS simulator or device the ddp call is returning raw html as mentioned by @banglashi. Hence the following error being thrown:

Uncaught Error: SyntaxError: JSON Parse error: Unrecognized token '<':2087:http://meteor.local/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706

Any point in the right direction would be awesome, would love to get cluster working in my cordova app. Thanks.

1nsaneinc commented 9 years ago

I'm getting a similar error on iOS Meteor-Cordova app running on iPhone 5, although this has nothing to do with Cluster.

Uncaught Error: SyntaxError: JSON Parse error: Unrecognized token '<':9:http://meteor.local/5a2b00d42a6ce9eb45c5c48f54778073184a8104.js

Not sure how to debug this, any pointers please?

patrickleet commented 9 years ago

Also getting this error - no solutions yet?

patrickleet commented 9 years ago

Or workarounds @1nsaneinc @fishdude @arunoda ?

patrickleet commented 9 years ago

Started a discussion here: https://forums.meteor.com/t/using-cluster-with-cordova/10678

patrickleet commented 9 years ago

Through discussions on the forum, looks like this is caused by connecting to an invalid URL, leading me to believe that Cluster.discoverConnection is returning an invalid endpoint when on cordova.

Manually using DDP.Connect works, but isn't as scalable.

patrickleet commented 9 years ago

I tried initializing the connection with DDP.connect a few ways, and they all work. 1) using root_url 2) using ip address from cluster discovery db 3) using balancer url from cluster discovery db

Either the lookup is failing, or the lookup is returning the wrong URL, and then failing when trying to connect with an invalid url.

MasterJames commented 9 years ago

Can you debug with weinre (Web inspector remote) or log it or just display the url in a text component etc. and hopefully test it from there too?

patrickleet commented 9 years ago

Ok, so here's a little more info, looks like all Cluster.discoverConnection does from a client is call DDP.connect with a '/' prepended to the name of the service passed in. https://github.com/meteorhacks/cluster/blob/master/lib/client/api.js#L2

I'm looking at Meteor's code now to see how that's handled. I don't really understand how that should work on web and not cordova. As is, I'm not even sure why would DDP.connect('/stores') would know that '/stores' is on a different server.

https://github.com/meteor/meteor/blob/devel/packages/ddp-client/livedata_connection.js#L52

patrickleet commented 9 years ago

Looks like the endpoint ("/" + serviceName) eventually ends up here: https://github.com/meteor/meteor/blob/devel/packages/ddp-client/stream_client_nodejs.js#L12

According the comments above the contstructor, supported endpoints are

// @param endpoint {String} URL to Meteor app
//   "http://subdomain.meteor.com/" or "/" or
//   "ddp+sockjs://foo-**.meteor.com/sockjs"

So the way that cluster is connecting is still a bit of a mystery to me. How does '/stores' even translate to connecting to a different server when on web?

If I could pick an endpoint on the client, and use the fully qualified url that would probably solve the problem, but I couldn't find a way to pick an endpoint on the client, only on the server.

patrickleet commented 9 years ago

@arunoda why does client only use DDP.connect, while the server uses ConnectionWatcher?

package.js suggests it could be used for both? https://github.com/meteorhacks/cluster/blob/master/package.js#L46

arunoda commented 9 years ago

Okay, this is how /service is working (or suppose to work).

This happens in the initial /sockjs/info call. See:

It will get the service and send a sockjs base_url which is appended to the actual sockjs WS connection. So, we can redirect sockjs traffic into some other service.

Seems like, it's having some issues in cordova.

arunoda commented 9 years ago

It seems like cordova directly use the Websocket endpoint bypassing sockjs. In order to cluster to work properly it needs sockjs.

I am not familiar with cordova code. So, need to dig more.

patrickleet commented 9 years ago

@arunoda I don't see how it would ever get there from the client.. looks like all that happens is DDP.connect: https://github.com/meteorhacks/cluster/blob/master/lib/client/api.js#L2

My pull request modifies ConnectionWatcher to work on the client as well. The only place it failed was getting the endpoint, so on the client I just have it call to the server to get the endpoint. Is there something wrong with that approach that will cause problems down the line?

Micjoyce commented 8 years ago

@arunoda @patrickleet
I have encountered this problem , it was solved?

Micjoyce commented 8 years ago

Very much hope that someone can give me some answers on this issue。

patrickleet commented 8 years ago

@Micjoyce yes, worked for me, and a few others on the forums have used the code from this PR to solve their issues as well. I have a app in production using this.

More here: https://forums.meteor.com/t/using-cluster-with-cordova/10678/3

Micjoyce commented 8 years ago

@patrickleet I did a test according to your method of app, but will still appear this question: Uncaught Error: SyntaxError: JSON Parse Error: Unrecognized token '<' : 2104: http://meteor.local/packages/ddp-client.js?250 b63e6c919c5383a0511ee4efbf42bb70a650f

this is my test app : https://github.com/Micjoyce/meteor-cluster-accounts-example