Skip to content
A pure JavaScript browser-friendly api for using dat
Branch: master
Clone or download
Latest commit 419cefc Mar 20, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
tests New api + hyperdrive 9 + websocket replication (#13) Feb 18, 2019
.travis.yml add node Dec 21, 2016
.zuul.yml Make browser friendly by using webrtc-swarm Feb 17, 2017 Fix typo (#19) Feb 25, 2019
index.js Fix opts not getting passed to dat.get() Mar 20, 2019
package.json Version 7.1.1 Mar 20, 2019
repo.js Change how storage works, update docs (#18) Feb 22, 2019

dat-js Travis NPM version

A pure JavaScript browser-friendly api for using dat.

Dat is a powerful decentralized data sharing tool. For a Node.js api for working with dats on the filesystem, see dat-node.

Want to use Dat in the command line or an app (not build applications)? Check out:

Learn more! or chat with us (#dat on IRC)


Getting data from a remote dat

var Dat = require('dat-js')

var dat = new Dat()
var repo = dat.get('dat://SOME_ARCHIVE_URL')

var readStream = repo.archive.readFile('hello.txt', function (err, data) {

Replicating a dat in memory

var Dat = require('dat-js')

var dat = new Dat()
var repo = dat.create()

console.log('dat url is:', repo.url)

// You can start reading/writing right away
var writer = repo.archive.createWriteStream('hello.txt')

writer.end(function () { replicate(repo.url) })

function replicate (url) {
  var clone = new Dat()
  var repo = clone.get(url)

  var readStream = repo.archive.createReadStream('hello.txt')
  readStream.on('data', function (data) {
    console.log(data.toString()) // prints 'world'

Replication with a websocket gateway

var Dat = require('dat-js')
var concat = require('concat-stream')
var pump = require('pump')

var dat = new Dat({
  gateway: 'ws://'
var repo = dat.get('dat://SOME_ARCHIVE_URL')

var readStream = repo.archive.createReadStream('hello.txt')

pump(readStream, concat(function (data) {

Persisting a created dat and loading it from storage

var Dat = require('dat-js')
var db = require('random-access-idb')('dats')

var dat = new Dat()

var repo = dat.create({
  db: db

repo.archive.writeFile('/example.txt', 'Hello World!', () => {
  // Save it for later
  localStorage.setItem('My_Repo', repo.url)

// Next time your app loads

var repo = dat.get(localStorage.getItem('My_Repo'), {
  db: db

repo.archive.readFile('/example.txt', 'utf-8', (err, data) => {
  console.log(`It's still there: ${data}`)


var dat = new Dat([options])

Creates a new dat object. The options passed here will be default for any dats created using the add method.

  • options: any options you can pass to mafintosh/hyperdrive. These options will become default for all dats. In addition it has the following:
  • signalhub: An optional string or array of strings for signalhubws servers to use for WebRTC. Note that this isn't compatible with older signalhub servers. These servers are used to discover and connect to WebRTC peers. Note that each signalhub constitutes a different WebRTC swarm. The default is
  • gateway: An optional string or array of strings for dat-gateway instances for websocket replication. This is used to proxy data from the rest of the dat network when there are no WebRTC peers available with your data. There is no gateway set by default, but you can use if you want it.

dat.get(url, [options])

Adds a new dat with the given url. Joins the appropriate swarm for that url and begins to upload and download data. If the dat was already added, it will return the existing instance. One gotcha is that dat-js doesn't support DNS resolution yet. As such you'll need to use the actual archive key for loading websites.

  • url: Either a dat:// url or just the public key in string form.
  • options: These options will override any options given in the Dat constructor.


Creates a new dat, wait for it to be ready before trying to access the url.

  • options: These options will override any options given in the Dat constructor.


Returns whether a given url has been loaded already.



Array of repo instances


The repo object managed by dat.


The dat:// url of the repo. Note that for newly created repos, you must wait for it to be ready.


Invokes the cb once the repo is fully initialized. You can do reads and writes from the archive before then, but this is important if you're creating a new archive. If the repo is already ready, it will invoke cb on the next tick.


Closes the swarm and underlying database.


Get the hyperdrive archive instance, where files can be managed using that api.


Get the webrtc-swarm instance, where the swarm can be managed.



Fired every time a new repo is ready.


Fired when dat is finished closing, including swarm and database.

You can’t perform that action at this time.