Architecture of using Sails API with AWS RDS behind ionic

I have an ionic front-end that I want to connect with an AWS relational database using the Sails framework. Can any one give me a general idea of how the file structure should look? I’m presuming my www folder goes inside Sails views folder. But can’t seem to get it wired up to render. Before bringing Sails in the front-end file structure looks like this;

app
    hooks
    node_modules
    platforms
    plugins
    scss
    www
      pageview1
      pageview2
      .
      .
      .
      libs
      app.js
      index.html
    .bowerrc
    .gitignore
    bower.json
    config.xml
    gulpfile.js
    ionic.project
    package.json

I ran sails new from the command line and inside app it added;

sails_app 
  .tmp
  api
  assets
  config
  node_modules
  tasks
  views
  .editorconfig
  .gitignore
  .sailsrc
  app.js
  gruntfile.js
  package.json

Seems off topic for an Ionic Forum.

I use Sailsjs as a backend to build out REST API’s for all my Ionic apps.

For Ionic you’ll just want to follow one of the $http tutorials from Ionic

For any Sails specific questions, I’d goto one of the suggested Sails support sites and ask the same question.

Also, rereading your question, it sounds like you want to put the Ionic app inside Sails? It wouldn’t work like that, you’d build your Sails app, put on server, then build your Ionic app to install on your device. The Ionic app would talk to the Sails server via http.

So an Ionic app wouldn’t follow the SPA pattern Sailsjs describes here?http://sailsjs.org/#!/documentation/concepts/Views
As an aside, I pinged the Sailsjs team on Twitter and their response was to “just toss [my ionic app] in the assets directory”.

If you want to serve your Ionic app in a browser for a normal web browser, a “web app”, sure that’s what you’d want to do.

However, if you want to build an app that is eventually installable from an app store no. The app would be independent from Sails and integrated with it via API calls to a REST API you incorporate into your Sails app.

I can assure you this is correct. It is exactly what I am doing now and really just a fundamental thing with app development. Apps are independent builds that communicate with backend servers via a REST or Socket API that runs on the server.

I think there might be some exceptions for things like meteor, but I am not familiar with them.

1 Like

Thanks @kevbaker. That’s exactly the configuration I’ve ended up with. I’m really loving working with Ionic and Sails together.

1 Like

@kevbaker I followed your advice on this. I have my Sails API served on an AWS EC2 instance and I’m using the Sails/socket.io client side libs in my ionic app.

If I run ionic serve the app connects to the API without issue and I made a test io.socket.get() request from the Chrome console and got the expected data back.

But if I run ionic run android and build to my device it fails to connect. When I inspect device from Chrome I can see it repeatedly trying to open the socket connection unsuccessfully. Any ideas why I get two different results from the same code?

Brad,

This is likely due to your network connection. When you are using ionic serve is it connecting to a local server or are you connecting directly to your AWS machine? If local, then you need to be sure that your Sails application is accessible from your Android device, either by IP or some sort of proxy settings on your device.

If you are connecting directly to your AWS EC2 instance from your Android device, there should be no network differences between ionic server and ionic run android from your device. In this case, I would try pulling up a browser on your android device and try to pull up any Sails GET endpoint, don’t forget the port.

The ionic app is served locally from my laptop with ionic serve. Building to the phone with ionic run android is using the phones network connection. In both cases I’m giving it the ip:port to connect to in the form of;

<script type="text/javascript"> io.sails.useCORSRouteToGetCookie = false; io.sails.url = 'ip:port/'; </script>.

in my index.html

I thought maybe it was because I had the .pem file with the AWS key on my laptop but not my phone. But my friend just pulled down the ionic app and did an ionic serve on his laptop. It connects for him. and he was able to do an

io.socket.get('myurl', function (data) { console.log(data) });

and gets the expected data back.