RhoConnect is a synchronization framework consisting of a client application (a client component on the mobile device) and a server component (a RhoConnect application) that runs on any server capable of running Ruby. The client application can be a Rhodes application, or another mobile application written without the use of Rhodes (such as an iOS/Objective C or Android/Java application).
In this tutorial, you will learn:
Full documentation of all capabilities of RhoConnect can be found in the RhoConnect Developer Guides.
You can install RhoConnect in two ways:
If you have been issued a RhoConnect license, you can license your app as described here.
Ruby RhoConnect apps can also be generated using RhoStudio. To use this method, see Generating a RhoConnect app with RhoStudio
After code generation, the bundle install command will be run for the first time automatically.
|It is required to have a connection to the internet to create an application. There are certain Ruby gems that RhoConnect is dependant on.|
Once you have generated your app code, you’ll need to start your redis server and the RhoConnect app in order to ensure that everything is working properly.
If all went well, you should see:
Listening on 0.0.0.0:9292, CTRL+C to stop
This means that your RhoConnect app is up and running on localhost:9292. If you go to this address in your browser you will see the RhoConnect Console, pictured below.
To demonstrate how this works we have setup a dummy backend for a
Product model. This backend will allow you to perform all CRUD operations, however the data will be reset every hour.
Let’s go ahead and create a source adapter for this model.
Now let’s replace the contents of the Source Adapter generated code. Normally, you will replace each method with code specific to your backend.
The above example will work with our test backend, if you are attempting to connect to your own backend data, you should read more about Source Adapters and the corresponding methods:
To test to make sure the sync is working, we need to create a basic RhoMobile application.
|Pay attention to spelling of the model and attributes, as the sample code is assuming that it will match exactly to the JSON object coming from the backend.|
1) Open the folder
app\Product and edit the file
2) Uncomment the line
enable :sync line.
# The model has already been created by the framework, and extends Rhom::RhomObject # You can add more methods here class Product include Rhom::PropertyBag # Uncomment the following line to enable sync with Product. enable :sync #add model specific code here end
1) Open the file
2) Replace the line:
<li><a href="#">Add link here...</a></li>
<div data-role="content"> <ul data-role="listview"> <li><a href="Product">Product</a></li> </ul> </div>
Make sure your server URL is configured in the Rhomobile app. If your server is running on YourIPAddress with the port 9292, the following line should be at the bottom of your corresponding Rhodes app,
syncserver = 'http://YourIPAddress:9292'
|If you are using Android, syncserver cannot be set to localhost, you must use your machine's local IP Address. Ex: 192.168.x.x|
To sync with the RhoConnect server, the Rhodes client must log in. The Rhodes generated app includes some screens for login and other common functions, which you will typically modify to suit the design of your application. The generated UI is useful since it allows you to focus on the core functionality of your application before implementing the important, but mundane, details of user authentication and settings.
1. From the home page in the Rhodes app, click on the login button in the upper right corner. Alternatively, you can click on the tool icon at the bottom of the screen to go to the Settings screen.
2. Login using any name & password. The generated code allows any login, but you can modify that in application.rb.
App Home Page
App Login Page
3. If login is successful, you will see a Client ID in the settings screen. The ClientID is generated the first time you log in and is stored in the client database. It serves as a unique identifier which is required for rhoconnect. (Note: this value will persist across logins, but if you reset the client database or the user re-installs the app, a new ClientID will be generated.)
4. Sync is triggered automatically. Click on the home icon and then select “Products” and you should see the list of product records from the server.
App Settings Page With Client ID
List of Products on Local Storage
5. You can now add, modify or delete products to your app locally and then sync them to the Rhostore back-end.
Create New Product Page
Rhostore Before Sync
Rhostore After Sync
6. You can also simulate changes that occur on the backend, by using the CRUD operations from your browser. These changes will not appear on the device for at least 5 minutes. This is due to the default setting for the RhoConnect application to query the backend for updates. You can modify this by changing the `poll_interval` setting in the `settings\setting.yml` file in the RhoConnect application.
Now that you have your RhoConnect server successfully syncing to your back-end application, you’re ready to move on to more advanced topics using RhoConnect such as PUSH notifications, RhoConnect plug-ins, and sync conflict resolution to name a few. For more information on these and other topics, refer to our RhoConnect Guides.