SlideShare a Scribd company logo
1 of 21
Download to read offline
How we built 
offline data access & sync 
in LoopBack 
Miroslav Bajtoš
What is LoopBack?
Data access & ORM 
● Own querying language inspired by 
MongoDB 
● Connectors for SQL, NoSQL and more 
● In-memory database 
Todo.find({ 
where: { completed: false }, 
limit: 10 
}, function(err, list) { /*...*/ });
REST API 
Built-in data-access methods 
Todo.find => GET /todos?filter=:filter 
Todo.create => POST /todos 
Todo.findById => GET /todos/:id 
(and so on) 
Custom methods 
Todo.stats => GET /todos/stats
LoopBack on the server 
Todo 
model 
REST 
API 
data 
source 
Mongo 
MySQL 
SOAP
The trouble with offline mode
Different tools & APIs 
Online 
● REST/HTTP calls 
● full power of back-end & 
database 
GET /todos?where= 
{"completed":false} 
&limit=10 
Offline 
● local storage/index db 
● key/value store 
JSON.parse( 
localStorage.todos 
).filter(function(t){ 
return 
!t.completed; 
}).slice(0,10);
Synchronization 
● What has been changed locally? 
● What has changed on the server? 
● How to detect conflicts?
The LoopBack recipe 
Single API for offline & online 
● LoopBack in the browser 
● LocalStorage as a database 
● REST server as a database 
Synchronization 
● Change replication between data-sources
Single API for online & offline
Welcome to the browser 
Node.js 
Todo 
model 
Browser 
data 
source 
Local 
storage 
LoopBack 
server
Remoting metadata 
Todo.find(filter, cb) { … } 
Todo.remoteMethod('find', { 
accepts: [{ 
arg: 'filter', type: 'object' }], 
returns: [{ 
arg: 'data', type: 'array', 
root: true }], 
http: { verb: 'get', path: '/' } 
}); 
Todo.sharedClass.http = { path: '/todos' }
Remoting implementation - server 
app.get('/todos', function(req, res, next) { 
Todo.find( 
req.param['filter'], 
function(err, data){ 
if (err) next(err) 
else res.send(data); 
} 
); 
}
Remoting implementation - client 
Todo.find = function(filter, cb) { 
request.get( 
{ 
path: '/todos', 
query: { filter: filter } 
}, 
function(err, res) { 
if (err) cb(err) 
else cb(null, res.body); 
}); 
}
The result 
One API to rule them all: 
● server 
● online browser 
● offline browser 
Todo.find({ 
where: { completed: false }, 
limit: 10 
}, function(err, list) { /*...*/ });
Data synchronization
Change tracking 
Change tracking record: 
modelName "Todo" 
modelId "42" 
revision "SHA of data" 
checkpoint 2 
● Updated immediately by loopback writes 
● Manual update for external writers 
(scheduled at a regular interval)
The replication algorithm 
1. Find local changes 
2. Compare remote and local changes, 
detect conflicts 
3. Resolve conflicts 
4. Perform a bulk update 
5. Create a new checkpoint
Browser 
Synchronizing client 
local 
Todo 
model 
data 
source 
Local 
storage 
LoopBack 
server 
change tracking & replication 
remote 
Todo 
model 
data 
source
DEMO!
THANK YOU! 
LoopBack 
http://loopback.io 
Slides 
http://bit.ly/sync-loopback 
Get in touch 
http://twitter.com/bajtos 
http://linkedin.com/in/bajtos

More Related Content

Viewers also liked

LoopBack: a productivity booster for MEAN
LoopBack: a productivity booster for MEANLoopBack: a productivity booster for MEAN
LoopBack: a productivity booster for MEANMiroslav Bajtoš
 
Picking the Right Node.js Framework for Your Use Case
Picking the Right Node.js Framework for Your Use CasePicking the Right Node.js Framework for Your Use Case
Picking the Right Node.js Framework for Your Use CaseJimmy Guerrero
 
Why I hated the daily stand-up and how I started enjoying it?
Why I hated the daily stand-up and how I started enjoying it?Why I hated the daily stand-up and how I started enjoying it?
Why I hated the daily stand-up and how I started enjoying it?Karthik Sirasanagandla
 
Building a Node.js API backend with LoopBack in 5 Minutes
Building a Node.js API backend with LoopBack in 5 MinutesBuilding a Node.js API backend with LoopBack in 5 Minutes
Building a Node.js API backend with LoopBack in 5 MinutesRaymond Feng
 
Toronto node js_meetup
Toronto node js_meetupToronto node js_meetup
Toronto node js_meetupShubhra Kar
 
Node.js Frameworks & Design Patterns Webinar
Node.js Frameworks & Design Patterns WebinarNode.js Frameworks & Design Patterns Webinar
Node.js Frameworks & Design Patterns WebinarShubhra Kar
 
How to run an effective (and fun) standup
How to run an effective (and fun) standupHow to run an effective (and fun) standup
How to run an effective (and fun) standupEd Kraay
 
Kanban in 4 easy steps
Kanban in 4 easy steps Kanban in 4 easy steps
Kanban in 4 easy steps Shore Labs
 

Viewers also liked (11)

Loopback
LoopbackLoopback
Loopback
 
LoopBack: a productivity booster for MEAN
LoopBack: a productivity booster for MEANLoopBack: a productivity booster for MEAN
LoopBack: a productivity booster for MEAN
 
Picking the Right Node.js Framework for Your Use Case
Picking the Right Node.js Framework for Your Use CasePicking the Right Node.js Framework for Your Use Case
Picking the Right Node.js Framework for Your Use Case
 
Why I hated the daily stand-up and how I started enjoying it?
Why I hated the daily stand-up and how I started enjoying it?Why I hated the daily stand-up and how I started enjoying it?
Why I hated the daily stand-up and how I started enjoying it?
 
Building a Node.js API backend with LoopBack in 5 Minutes
Building a Node.js API backend with LoopBack in 5 MinutesBuilding a Node.js API backend with LoopBack in 5 Minutes
Building a Node.js API backend with LoopBack in 5 Minutes
 
Toronto node js_meetup
Toronto node js_meetupToronto node js_meetup
Toronto node js_meetup
 
Node.js Frameworks & Design Patterns Webinar
Node.js Frameworks & Design Patterns WebinarNode.js Frameworks & Design Patterns Webinar
Node.js Frameworks & Design Patterns Webinar
 
How to run an effective (and fun) standup
How to run an effective (and fun) standupHow to run an effective (and fun) standup
How to run an effective (and fun) standup
 
Scrum In 15 Minutes
Scrum In 15 MinutesScrum In 15 Minutes
Scrum In 15 Minutes
 
Kanban in 4 easy steps
Kanban in 4 easy steps Kanban in 4 easy steps
Kanban in 4 easy steps
 
Lokijs
LokijsLokijs
Lokijs
 

How we built offline data access & sync in LoopBack

  • 1. How we built offline data access & sync in LoopBack Miroslav Bajtoš
  • 3. Data access & ORM ● Own querying language inspired by MongoDB ● Connectors for SQL, NoSQL and more ● In-memory database Todo.find({ where: { completed: false }, limit: 10 }, function(err, list) { /*...*/ });
  • 4. REST API Built-in data-access methods Todo.find => GET /todos?filter=:filter Todo.create => POST /todos Todo.findById => GET /todos/:id (and so on) Custom methods Todo.stats => GET /todos/stats
  • 5. LoopBack on the server Todo model REST API data source Mongo MySQL SOAP
  • 6. The trouble with offline mode
  • 7. Different tools & APIs Online ● REST/HTTP calls ● full power of back-end & database GET /todos?where= {"completed":false} &limit=10 Offline ● local storage/index db ● key/value store JSON.parse( localStorage.todos ).filter(function(t){ return !t.completed; }).slice(0,10);
  • 8. Synchronization ● What has been changed locally? ● What has changed on the server? ● How to detect conflicts?
  • 9. The LoopBack recipe Single API for offline & online ● LoopBack in the browser ● LocalStorage as a database ● REST server as a database Synchronization ● Change replication between data-sources
  • 10. Single API for online & offline
  • 11. Welcome to the browser Node.js Todo model Browser data source Local storage LoopBack server
  • 12. Remoting metadata Todo.find(filter, cb) { … } Todo.remoteMethod('find', { accepts: [{ arg: 'filter', type: 'object' }], returns: [{ arg: 'data', type: 'array', root: true }], http: { verb: 'get', path: '/' } }); Todo.sharedClass.http = { path: '/todos' }
  • 13. Remoting implementation - server app.get('/todos', function(req, res, next) { Todo.find( req.param['filter'], function(err, data){ if (err) next(err) else res.send(data); } ); }
  • 14. Remoting implementation - client Todo.find = function(filter, cb) { request.get( { path: '/todos', query: { filter: filter } }, function(err, res) { if (err) cb(err) else cb(null, res.body); }); }
  • 15. The result One API to rule them all: ● server ● online browser ● offline browser Todo.find({ where: { completed: false }, limit: 10 }, function(err, list) { /*...*/ });
  • 17. Change tracking Change tracking record: modelName "Todo" modelId "42" revision "SHA of data" checkpoint 2 ● Updated immediately by loopback writes ● Manual update for external writers (scheduled at a regular interval)
  • 18. The replication algorithm 1. Find local changes 2. Compare remote and local changes, detect conflicts 3. Resolve conflicts 4. Perform a bulk update 5. Create a new checkpoint
  • 19. Browser Synchronizing client local Todo model data source Local storage LoopBack server change tracking & replication remote Todo model data source
  • 20. DEMO!
  • 21. THANK YOU! LoopBack http://loopback.io Slides http://bit.ly/sync-loopback Get in touch http://twitter.com/bajtos http://linkedin.com/in/bajtos