 # There is a corresponding implementation run by clients.
 #---------------------------------------------------------------------
 
+###
+x={};require('bup_server').global_client(cb:(e,c)->x.c=c; x.s = require('syncstring'); x.s.init_syncstring_db(x.c.database); x.ss=x.s.syncdb(string_id:'4bd8cb98-506c-45a5-8042-5c6bd8fddff1', cb:(e,t)->x.t=t))
+###
 _syncdb_cache = {}
 exports.syncdb = (opts) ->
     opts = defaults opts,
         string_id      : required
+        max_length     : MAX_STRING_LENGTH
         cb             : required
     d = _syncdb_cache[opts.string_id]
     if d?
         opts.cb(undefined, d)
         return
-    get_syncstring opts.string_id, (err, S) =>
-        if err
-            opts.cb(err)
-        else
-            doc = new diffsync.SynchronizedDB_DiffSyncWrapper(S.in_memory_client)
-            S.db_client.on 'changed', () =>
-                doc.emit("sync")
-            d = _syncdb_cache[opts.string_id] = new diffsync.SynchronizedDB(doc)
-            d.string_id = opts.string_id
-            opts.cb(undefined, d)
-
-
+    get_syncstring
+        string_id  : opts.string_id
+        max_length : opts.max_length
+        cb         : (err, S) =>
+            if err
+                opts.cb(err)
+            else
+                doc = new diffsync.SynchronizedDB_DiffSyncWrapper(S.in_memory_client)
+                S.db_client.on 'changed', () =>
+                    doc.emit("sync")
+                d = _syncdb_cache[opts.string_id] = new diffsync.SynchronizedDB(doc)
+                d.string_id = opts.string_id
+                opts.cb(undefined, d)
+
+

        string_id      : required
        session_id     : required
        push_to_client : required    # function that allows for sending a JSON message to remote client
        cb             : required
    get_syncstring opts.string_id, (err, S) =>
        if err
            opts.cb(err)
        else
            S.new_browser_client
                session_id     : opts.session_id
                push_to_client : opts.push_to_client
                cb             : (err, client) ->
                    if err
                        opts.cb(err)
                    else
                        opts.cb(undefined, client)

# The hub has to call this on startup in order for syncstring to work.
syncstring_db = undefined

                #dbg("nothing to do for #{string_id}")
                cb() # nothing to do
            else
                t0 = misc.mswalltime()
                #dbg("starting patch make from length #{string.last_sync.length} to #{string.live.length}...")
                patch = diffsync.dmp.patch_make(string.last_sync, string.live)
                #dbg("made patch for #{string_id} in #{misc.mswalltime(t0)}ms"); t0 = misc.mswalltime()
                #dbg("patch for #{string_id} = #{misc.to_json(patch)}")
                timestamp = cass.now() - 0
                patch_as_string = @patch_to_string(patch)
                #dbg("converted patch to string in #{misc.mswalltime(t0)}ms"); t0 = misc.mswalltime()
                @db.update
                    table : 'syncstrings'
                    set   : {patch:patch_as_string}
                    where : {string_id:string_id, timestamp:timestamp}
                    cb    : (err) =>
                        #dbg("wrote patch to database in #{misc.mswalltime(t0)}ms"); t0 = misc.mswalltime()
                        if err
                            cb(err)
                        else
