| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
This library provides a simple http route handler, along with client code, enabling fast text synchronization over a standard protocol.
This library makes it safe, easy & efficient to add collaborative text editing to every user-editable string in your web app. Make your app multiplayer!
Check out the demo video 📺 from the Braid 86 release!
This will run a collaboratively-editable wiki:
npm install
node server-demo.jsNow open these URLs in your browser:
Or try opening the URL in Braid-Chrome, or another Braid client, to edit it directly!
Check out the server-demo.js file to see examples for how to add simple access control, where a user need only enter a password into a cookie in the javascript console like: document.cookie = 'password'; and a /pages endpoint to show all the edited pages.
Install it in your project:
npm install braid-textImport the request handler into your code, and use it to handle HTTP requests wherever you want:
var braid_text = require("braid-text")
http_server.on("request", (req, res) => {
// Your server logic...
// Whenever desired, serve braid text for this request/response:
braid_text.serve(req, res)
})braid_text.db_folder = './braid-text-db' // <-- this is the default
braid_text.serve(req, res, options)
braid_text.sync(local_key, remote_url, options)
await braid_text.get(key)
await braid_text.get(key, options)
await braid_text.put(key, options)
Here's a basic running example to start:
<!-- 1. Your textarea -->
<textarea id="my_textarea"></textarea>
<!-- 2. Include the libraries -->
<script src="https://unpkg.com/braid-http@~1.3/braid-http-client.js"></script>
<script src="https://unpkg.com/braid-text@~0.3/client/simpleton-sync.js"></script>
<!-- 3. Wire it up -->
<script>
// Connect to server
var simpleton = simpleton_client('https://braid.org/public-sandbox', {
on_state: state => my_textarea.value = state, // incoming changes
get_state: () => my_textarea.value // outgoing changes
})
// Tell simpleton when user types
my_textarea.oninput = () => simpleton.changed()
</script>You should see some text in a box if you run this, and if you run it in another tab, you should be able to edit that text collaboratively.
The client uses a decoupled update mechanism for efficiency:
This design prevents network congestion and handles disconnections gracefully. For example, if you edit offline for hours, the client will send just one efficient diff when reconnecting, rather than thousands of individual keystrokes.
For better performance and control, you can work with patches instead of full text:
Each patch is an object with two properties:
Patches in an array each have positions which refer to the original text before any other patches are applied.
Instead of receiving complete text updates, you can process individual changes:
var simpleton = simpleton_client(url, {
on_patches: (patches) => {
// Apply each patch to your editor..
},
get_state: () => editor.getValue()
})This is more efficient for large documents and helps preserve cursor position.
You can provide your own diff algorithm or use patches from your editor's API:
var simpleton = simpleton_client(url, {
on_state: state => editor.setValue(state),
get_state: () => editor.getValue(),
get_patches: (prev_state) => {
// Use your own diff algorithm or editor's change tracking
return compute_patches(prev_state, editor.getValue())
}
})See editor.html for a complete example.
This will render each peer's cursor and selection with colored highlights. Just add three lines to your simpleton client:
<!-- 1. Include these additional script tags -->
<script src="https://unpkg.com/braid-text@~0.3/client/textarea-highlights.js"></script>
<script src="https://unpkg.com/braid-text@~0.3/client/cursor-sync.js"></script>
<!-- 2. Add two lines to your simpleton_client() call -->
<script>
var cursors = cursor_highlights(my_textarea, location.pathname)
var simpleton = simpleton_client(location.pathname, {
on_patches: (patches) => {
apply_patches_and_update_selection(my_textarea, patches)
cursors.on_patches(patches) // <-- update remote cursors
},
get_state: () => my_textarea.value
})
my_textarea.oninput = () => {
cursors.on_edit(simpleton.changed()) // <-- send local cursor
}
</script>simpleton = simpleton_client(url, options)Creates a new Simpleton client that synchronizes with a Braid-Text server.
Parameters:
() => current_text_stringon_state: [optional] Callback for receiving complete state updates
(state) => { /* update your UI with new text */ }on_patches: [optional] Callback for receiving incremental changes
(patches) => { /* apply patches to your editor */ }Each patch has:
Note: All patches reference positions in the original text before any patches are applied.
get_patches: [optional] Custom function to generate patches
(previous_state) => array_of_patchesIf not provided, uses a simple prefix/suffix diff algorithm.
Note: All patches must reference positions in the original text before any patches are applied.
The following options are deprecated and should be replaced with the new API:
cursor_highlights(textarea, url) returns an object with:
Colors are auto-assigned per peer ID. See ?editor and ?markdown-editor in the demo server for working examples.
first run the test server:
npm install node test/server.js
then open http://localhost:8889/test.html, and the boxes should turn green as the tests pass.
npm install node test/test.js
if the last output line looks like this, good:
t = 9999, seed = 1397019, best_n = Infinity @ NaN
but it's bad if it looks like this:
t = 9999, seed = 1397019, best_n = 5 @ 1396791
the number at the end is the random seed that generated the simplest error example
| Back | FazBrowse Home | New Git URL |