Quick start

Artur Drobinskiy edited this page Aug 13, 2017 · 11 revisions

In order to use Realmius you need to set up Realm on the client-side and Entity Framework on the server-side.

Server

  1. Install-Package Realmius.Server
  2. Inherit your DbContext from ChangeTrackingDbContext:
public class MyDbContext : DbContextChangeTrackingDbContext
  1. All your syncable EF-entities should implement IRealmiusObjectServer:
    public class Message : IRealmiusObjectServer //assume Message is a class backing up the Message table
    {
        public string Id { get; set; }

        public string MobilePrimaryKey => Id;

        public string Text { get; set; }

    }
  1. Initialize the synchronization process from the Configure method of OWIN Startup class of your web application (if there's no Startup class go ahead and Add New Item -> Owin Startup details)

RealmiusServer.SetupShareEverythingSignalRServer(
                "/Realmius",                        // URL Realmius server will be started on (also URL the client will connect to)
                app,                                // this is ASP.Net's IAppBuilder
                () => new MyDbContext(),            // factory-method which will create your DbContext
                typeof(Message)                     // types you want to be synchronized (you can add as many as you wish)
);

Now just create new Message objects and Save them via EntityFramework as you usually do. They will be automatically synced to the clients as they connect.

The example will share all the Messages between all the clients! For setting up advanced authentication and row level security continue to Advanced section.

Client

  1. Install-Package Realmius (if it's a PCL, make sure it's Profile 111/76. if it's not Xamarin project make sure it's at least .NET 4.6.1)
  2. All your syncable Realm entities should implement IRealmiusObjectClient and have a class name exactly matching classname on the server:
    public class Message : RealmObject, IRealmiusObjectClient
    {
        public string Id { get; set; }

        public string MobilePrimaryKey => Id;

        public string Text { get; set; }

    }
  1. Initialize the synchronization process:

SyncServiceFactory.CreateUsingSignalR(
                () => Realm.GetInstance(),            // factory-method which will create your Realm instance
                new Uri("http://localhost/Realmius"), // URL the client will connect to
                new[]
                {
                    typeof(Message)                   // types you want to be synchronized (you can add as many as you wish)
                });

You are done!

Now you can query objects, that were added from the server, or add objects to your realm and they will be synced to the server automatically!

Note: to remove objects you have to call Realmius method manually (since Realm doesn't provide any meaningfull change-notification for removed objects)

    realm.Write(() =>
                {
                     realm.RemoveAndSync(obj); //RemoveAndSync instead of realm.Remove(obj);
                });

For syncing references head over to Advanced section

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.