News for hackers. Stuff that matters.
Re:Hoodie: very fast web app development by lifeisstillgood on Monday April 08, 2013 @07:06PM (#5515331)
hi

I am trying very hard not to be dumb, but this is beating me

seemingly Kanso? and Hoodie both have JS stubs at the client and server end, these synch with each other, then synch with whatever you are using at client (say backbone) and server (node? CouchDB? My Postgres server behind my python app?)

If thats roughly right, why do I do this? I control my server apps no? Why wrap them in another layer? Json->backbone is hardly a difficult synch operation - I mean its called synch().

I really am interested in understanding the philosophy and driving ideas here - everyone has a moment of clarity where they can see how to build an app or a framework, and how it will come together - I am not attacking you (yet !) - please describe your moment of clarity.

Re:Hoodie: very fast web app development by Lazare on Monday April 08, 2013 @07:44PM (#5515490)
Okay, first off, let's discuss a standard three tier webapp architecture:

    Browser   <--->   Server   <--->   DB
You get some HTML/CSS/JS from the server, you display stuff to the user. As the user interacts with your app, HTTP requests hit your server which processes them. As needed, the server will make requests to the DB, which will reply with data, which then gets passed back to the browser. Everyone knows how this stack works, I hope. :)

It's simple and it justs works. Almost every website you use works this way. But for webapps, it's not always ideal. Let's discuss some of the issues:

First off, this architecture is "always online". If the browser loses connection to the server, then when your user clicks on a button nothing will happen. Not too bad in a desktop environment, but with laptops or - especially - mobile clients this isn't ideal.

Worse, what if you're trying to keep the state in sync with multiple clients (like Trello does)? You're probably using websockets or long polling or similar techniques so that the server can tell clients about the actions of other clients. A network hiccup might cause you to miss one of these updates; detecting and recovering from this sort of sync error is problematic. Generally the solution is to throw away the last few bits of work the user did, and force a full reload and re-fetch the current state. Which is fine, in most cases. Unless the work the user is difficult to replicate, or if the application state is quite large. Trello takes a second or two to grab the current cards; if your app takes 30s to grab its data, you may want to avoid this though. :)

In fact, the more you want to replicate the feel and functionality of a classic desktop app, while allowing multiple users and clients, the more you start to struggle against this architecture.

What other architectures are there? Well, there's one obvious one:

    Browser   <--->   CouchDB   <--->   Workers
CouchDB is kind of unique among databases because it speaks HTTP natively. It can serve your app to the client, and then turn around and have the clients connect directly to it. It can't, eg, send emails, but what you can do is use CouchDB as a queue. Add a document that says "send an email to X", then you spin up some worker processes that monitor it and process any documents like that as they appear.

CouchDB also has a _changes feed that makes a lot of sync tasks very easy. It also does master/master replication, and is generally a really nice database to work with from a devops point of view. Still, we're missing a crucial bit of magic to really improve over a classic three tier webapp. And that is...

...local DBs, in the form of PouchDB for browsers or TouchDB (apparently called Couchbase Mobile these days, although confusingly it's unrelated to Couchbase, which in turn is unrelated to CouchDB. Don't ask.) for Android/iOS. CouchDB is really really good at syncing and replicating, so now we have a new architecture:

    Browser/Mobile App  <-->  PouchDB/TouchDB  <-->  CouchDB  <-->  Workers
This isn't appropriate for every task. For one thing, it's quite a lot more work to implement (partly due to inferior tooling which projects like Hoodie are, thankfully, addressing). But if you really want the best possible user experience, you want it to work even if your on an airplane or in a tunnel, you want a really snappy UI, you want each client to see the changes made by other clients in as close to real time as practical, you want a consistent experience for native mobile apps and webapps running in a browser...then this is the architecture for you.

So to directly answer your questions:

First, Kanso (by default) wants to do something like:

   Browser  <-->  CouchDB
Which is a really slick, really easy way to do single page applications that don't need local persistence or server processes. But it's not hard to add them on, in which case you have the architecture above. You seem to be imagining something like this:

   Hoodie Client  <-->  Hoodie Server <-->  CouchDB
But that's not the idea at all. You're also asking why not do this:

  Backbone  <--->  Python   <--->  Postgres
And that's a perfectly fine architecture. It works, it's rock solid, and it's easy to code. For many apps (maybe most) it's the best possible architecture (well, I'd suggest Knockout over Backbone, but that's personal preference).

But for the project I'm working on right now, we decided that wasn't quite good enough. :)

Re:Hoodie: very fast web app development by jchrisa on Tuesday April 09, 2013 @01:46AM (#5516641)
Great answer. Tiny nitpick: the next version of TouchDB is called Couchbase Lite. We call it that b/c it is made by Couchbase and is lighter than TouchDB.

Couchbase for Mobile is what we call the umbrella (including our Sync Gateway which adds multi-user access control with more scale and flexibility than CouchDB, but compatible with the same sync protocol).

Of course it's all open source with a very active community. Join us here: https://groups.google.com/forum/m/#!forum/mobile-couchbase

Re:Hoodie: very fast web app development by Lazare on Tuesday April 09, 2013 @02:34AM (#5516747)
Thanks for the correction.
Re:Hoodie: very fast web app development by lifeisstillgood on Tuesday April 09, 2013 @03:11AM (#5516843)
Wow, thank you.

So from my little perspective, couchdb does replication over network, you are leveraging that to create apps that can queue network events locally and get on with other work, and that one definition of a record can easily be used in JS, local store and remote.

Seems nice. I remember another thread recently with a discussion on replacing the standby icon. At some point someone drew up a series of dynamic I ins showing network state - fully synched, uploading, fetching etc.

Your project is addressing a new and previously unknown need for users to have a mental model of - remote synch.

Always be working in growth areas :-)

My homework (along with everything else !)

1. How does couchdb replicate and what are its failure points (nothing is magic but you seem to like it - I will have to have a look. I assume erlang is not running in IOS - I will read up

Thank you for taking the time to cudgel new knowledge into an old brain

Cheers

Re:Hoodie: very fast web app development by Lazare on Tuesday April 09, 2013 @04:30AM (#5517001)
CouchDB's replication is nifty because, in a sense, there is no replication. Or rather, not in the sense that MySQL or MongoDB might talk about replication as a separate process or separate protocol.

I spent a while typing of descriptions of how it works, but honestly I think you're best served by these two answers:

http://stackoverflow.com/a/4766398/17086

https://github.com/couchbaselabs/TouchDB-iOS/wiki/Replicatio...

It's not magic, no. It does, however, work a lot like git (a good thing!). The biggest downside is that it pushes some of the work to the client; you really do have to figure out how to handle conflicts in some cases. For example, if you edit the same field on the same doc in different ways on two different clients and then try to sync, there is no automatic way to resolve that other than throwing one edit away (which you can totally do if that works for your problem space). Luckily, that's not a very common situation. :) And, again, how else are you going to solve something like that? It all comes back to the CAP theorem - you can have at most two of consistency, availability, and partition tolerance. Rich mobile apps can work around a lack of consistency if they must, but a lack of availability and partition tolerance undermines the "mobile" part; for some projects (like mine) that is unacceptable.

As for TouchDB, if memory serves it actually was running an erlang VM at one point, but has long since been rewritten into native Objective C (for the iOS version) and Java (for the Android version). The PouchDB version is written purely in Javascript. Again, all that matters in the end is that it speaks the protocol. As @caolan put it, CouchDB is not a database:

http://caolanmcmahon.com/posts/couchdb_is_not_a_database/

It's a protocol. :)

Re:Hoodie: very fast web app development by lifeisstillgood on Tuesday April 09, 2013 @09:22AM (#5517861)
Thank you.
Re:Hoodie: very fast web app development by nthnclrk on Tuesday April 09, 2013 @04:51AM (#5517042)
For someone just starting to code, and looking specifically to work on web apps, this was such a well articulated response. I really appreciated it.
Re:Hoodie: very fast web app development by gr2m on Monday April 08, 2013 @07:57PM (#5515535)
Lazare, this is awesome, thanks so much! Let us know if you'd like to look closer into Hoodie, happy to assist
Re:Hoodie: very fast web app development by pjbrunet on Tuesday April 09, 2013 @01:44AM (#5516638)
If my phone has no signal I'd rather see an error message. I don't want the app to pretend everything is fine when that's not the case. If I press "Delete" in the app, don't pretend the thing is deleted if it's not really deleted yet, that's deceptive. What if I take off on my boat and I press "Send" and your app says "Sent!" but it's really not sent because now I've left the coast--your app just lied to me.
Re:Hoodie: very fast web app development by Lazare on Tuesday April 09, 2013 @02:56AM (#5516801)
With all due respect, I think you're wrong.

If I'm writing an email in the Gmail app on Android, what I want when I hit send is for the app to save the email locally, and then do its best to try and deliver it. Ditto for, basically, every other app on the phone that talks to a remote server. If I try and send a tweet, I want it to go "sure", and then be able to go on about my business while it works in the background. (And, again, that's how the actual twitter app works.)

And if I'm using some app that lets me take notes about a meeting or interview and syncs them with the cloud, I sure as HELL want to be able to take those notes and save them locally, even if the cloud sync had to be delayed. "Sorry Mr. CEO, but I can't seem to get wifi in this meeting room. Can we go out on the balcony and you just run back over those last few points?"

Now, yeah, I need some way of checking to see if it has actually sent the email or whatever. And if it fails, a notification that it has done so is not amiss. Mobile apps, typically, already do this sort of thing. (For gmail for android, you can check the outbox to look for unsent emails, and I believe twitter for android will notify you if it fails to send a tweet.)

To the extent that you're just saying that the app needs to have a UI which doesn't lie or surprise the user, then I agree wholeheartedly. If the user cares whether or not the cloud has been updated, or the message has actually been sent, don't lie and claim it has when it hasn't!

But if you're saying that as a general rule we need to not handle network sync stuff in the background, I think that's nuts. Nobody wants to sit there with an email draft open hitting send every so often to see if the 3G signal is good enough to talk to the mail server yet. Nor do we want to save a bunch of emails as drafts when offline, then have to go open each one when we get net access again and send them. (Come to that, in your model, could we even save an email as a draft in gmail without net access? Remember, the drafts folder is synced. Do we want the app to claim that a draft has been saved when it's not accessible from your desktop gmail client? Isn't that another case of "pretending we've saved a draft when it's not really saved"? If not, why not?)

TL;DR: Good UI that doesn't lie to the user is important. That's not a good reason to cripple your apps functionality so it doesn't do anything you can lie about.

Re:Hoodie: very fast web app development by pjbrunet on Tuesday April 09, 2013 @04:34PM (#5521138)
You think I'm wrong but you wholeheartedly agree? Funny. Every situation is unique but bottom line: you can't receive content without a connection. Most apps NEED a connection to function. So an error message "No Connection, Retry?" is both common and expected behavior.
Re:Hoodie: very fast web app development by gr2m on Tuesday April 09, 2013 @04:53AM (#5517051)
and again, thanks for another thoughtful response.
Re:Hoodie: very fast web app development by nborwankar on Tuesday April 09, 2013 @03:20AM (#5516859)
Well the problem here is not what the app did - it's what it said. If offline it should say "Offline - queued for deletion" or "Offline - queued for sending". When you have offline mode you have to think differently and design your UX appropriately. You can't just pretend you're online and play fast and loose with how you inform the user of state. Sorry but your example is a red herring. Hope this clarifies.
Re:Hoodie: very fast web app development by jchrisa on Tuesday April 09, 2013 @01:51AM (#5516653)
Dropbox and iCloud have the same issue. They seem to be accepted by users. It is also possible to get status information telling you if you are fully synced or not.
Re:Hoodie: very fast web app development by espylaub on Monday April 08, 2013 @08:27PM (#5515676)
Wow, thanks a lot for that, spot on!
Re:Hoodie: very fast web app development by janl on Monday April 08, 2013 @08:07PM (#5515589)
Nailed it!
Re:Hoodie: very fast web app development by onethree on Monday April 08, 2013 @08:54PM (#5515777)
thats a fantastic explanation, thanks
Re:Hoodie: very fast web app development by espylaub on Monday April 08, 2013 @08:19PM (#5515649)
"Moment of clarity" is a great cue. To me, Hoodie is enormously empowering (which is why I'm part of the Hoodie team). As a self-taught, non comp-sci frontend dev with a humanities degree I can get all the backend/DB stuff sorted, definitely, but I'd actually rather not. It's difficult and occasionally arcane. I don't enjoy it. And, let's face it, I can make terrible mistakes. With Hoodie, I can quickly build apps by myself, on a solid, proven base, with super-fast setup, easy deployment, offline capability and syncing. And these last two don't even need to be actively invoked, they're just there and they just work. That's practically magical from where I stand.

Hoodie allows me to build things in days that would usually take me weeks and in some cases require additional people. This gives me more time to experiment, refine, user test etc. It also lets me delegate with ease: I don't want or need full control and responsibility over the server and the db and many security aspects, not for what I do. I'll happily pay nodejitsu and iriscouch for that, because they know more about this stuff than I could ever learn in the time it takes me to make the money to pay them.

That was my moment of clarity: Hoodie lets me get on with what I want to do and what I'm good at, and it's really got my back on many of the things I have neither the skill nor the patience nor the money for.

Re:Hoodie: very fast web app development by k__ on Tuesday April 09, 2013 @02:42AM (#5516769)
> on a solid, proven base

[Citation Needed]

Re:Hoodie: very fast web app development by espylaub on Tuesday April 09, 2013 @04:56AM (#5517055)
Ah, sorry, I was referring to couchDB (which does sync and offline for us), not Hoodie itself. That's still very much under construction, of course.
Re:Hoodie: very fast web app development by janl on Monday April 08, 2013 @07:13PM (#5515363)
p2p sync is actually a hard problem, you might want to look into the theory :)

The value of Hoodie is putting everything together into a way that a jQuery / backbone dev can be productive with.

It sounds like you are way more advanced, as such, you will have a way easier time managing all the pieces.

> Why?

Because we like building apps that way :)

Re:Hoodie: very fast web app development by nborwankar on Tuesday April 09, 2013 @03:36AM (#5516891)
Hi Jan,

another way to think about this (from the old days) is that disconnected mode data is a given because the rate of growth of data is far greater than the rate of growth of bandwidth. So everyone can't always be accessing all their data in the cloud through an always on infinite bandwidth connection.

So you're going to need to carry a subset of your data with you - the subset that's "hot".

Now having said that keeping hot data in sync with the larger data store is a hard problem - p2p or not.

And what these architectures (CouchDB, PouchDB, ...) do is take the sync problem and make it an infrastructure issue so it only has to be solved once by the infrastructure creator (CouchDB team) rather than again and again and again by each app developer who wakes up in the middle of the night and realizes they have to solve "sync" in their app as an application problem. Then they have a nightmare and when they wake up they are babbling.

The additional anti-pattern-badness with sync-in-the-app is that that kind of sync is usually incompatible with another app's bespoke sync. But when sync is in the infrastructure then a much larger group of people who use that infrastructure can share data across apps if they want to without having necessarily to share a schema. This is the real power of making sync part of the underlying computing fabric.

Finally when this infrastructure is open source the real -danger of lock-in such as when you use Dropbox is mitigated, should you want to have that freedom.

Cheers.

Re:Hoodie: very fast web app development by lifeisstillgood on Monday April 08, 2013 @07:29PM (#5515431)
peer-to-peer synch? So this is not client server? But browser to browser? WebRTC-enabled? Much cooler. And not even eventually consistent. Please point me at theories :-)

  jQuery / backbone dev can be productive with
I am not clear on that - is that "someone who does not / cannot develop on a tradiational server backend (LAMP etc). How does Hoodie help them? How do you handle network downtime or two clients changing one backend resource with downtime?

Why? - that was more what are the benefits to me as a LAMP / backbone dev to using Hoodie, as opposed to are you enjoying yourself, which seems evident :-)

Re:Hoodie: very fast web app development by janl on Monday April 08, 2013 @07:34PM (#5515454)
this is many-client-server. p2p sync enables you opening your Hoodie app on your browser, smartphone and tablet at the same time and make it all work, even when some or all of the devices are offline for short or long periods of time :)

> I am not clear on that - is that "someone who does not / cannot develop on a tradiational server backend (LAMP etc). How does Hoodie help them

  $ hoodie new app
  $ cd app
  $ jitsu create
  $ jitsu push
(simplified)

> How do you handle network downtime?

Waiting.

> or two clients changing one backend resource with downtime?

p2p sync with conflict detection as provided by CouchDB.

> that was more what are the benefits to me as a LAMP / backbone dev to using Hoodie, as opposed to are you enjoying yourself, which seems evident :-)

While you are capable of doing all the heavy lifting, why not focus on building great apps? Or if your thing is running backends for people who build the apps, why not give them Hoodie and take the day off? :)

Re:Hoodie: very fast web app development by tarabukka on Tuesday April 09, 2013 @06:39AM (#5517238)
> p2p sync enables you...

That isn't what "peer to peer" means, though. Peer to peer means that peers (the aforementioned browser, smartphone and tablet) communicate directly with each other.

Also, how does CouchDB keep things secure if clients can sync apparently any data? I'm assuming there's something there, but nobody else has explained this.

Re:Hoodie: very fast web app development by Lazare on Tuesday April 09, 2013 @08:38AM (#5517632)
The trick is twofold:

First, CouchDB supports security and validation.

Second, for CouchDB replication is just another client connecting to a DB; it's handled the same way anything else is.

As a result, we have the notion of filtered replication. The server-side CouchDB won't tell anyone secrets they don't need to know - be they clients or client-side DBs. At at the other end, the client-side CouchDB has some validation to stop "bad" data going into the database by accident, and then the server-side CouchDB has the same validation again. People can compromise their client DB all they want, but eventually it all ends up on the wire as plain old HTTP requests interacting with a plain old REST API.

In many ways - as far as security goes - the client DB is a red herring. The server ONLY speaks a well-defined REST API, and has validation and security to deal with malformed or malicious API requests. The fact that those requests are generated by a client-side DB based on data entered into it via JS commands is neither here nor there; a Backbone app would generate the exact same requests based on more-or-less the same JS commands. If you can secure any REST API, you can secure CouchDB.

Re:Hoodie: very fast web app development by janl on Tuesday April 09, 2013 @09:35AM (#5517922)
Yet again, spot on, thanks @Lazare!