DEV Community

raghab
raghab

Posted on • Originally published at raghab.xyz

instant distributed database- gun.js

GUN is like a super cool toolkit for building apps that need to share data securely. It’s all about keeping things decentralized and private, allowing developers to create applications similar to popular services like Dropbox. With GUN, you can work with different types of data, from simple values to complex documents and videos, making it really versatile. Plus, it works offline and ensures that your data stays encrypted and safe. Many developers love it for creating alternatives to mainstream apps, and there’s a great community behind it, making it even better!

it is easy to import in your project

<script src="https://cdn.jsdelivr.net/npm/gun/gun.js"></script>
Enter fullscreen mode Exit fullscreen mode

here we have a basic example using gun to add an entry and listen to it.

const key = 'sample-key';
const data = {
  message: 'Hello, GUN!'
};

// Store data in the GUN database
gun.get(key).put(data, ack => {
  if (ack.err) {
    console.error('Error storing data:', ack.err);
  } else {
    console.log('Data stored successfully!');
  }
});

// Retrieve data from the GUN database in realtime
gun.get(key).on(storedData => {
  if (storedData) {
    console.log('Retrieved data:', storedData);
  } else {
    console.log('No data found for the key:', key);
  }
})
Enter fullscreen mode Exit fullscreen mode

That is all you need to get started with gun!

gunjs provides an incredibly simple interface for programming and eases out most of the headaches, such ss authentication. in the next example you’ll see how easy it is to authenticate with gunjs.

const gun = Gun();
const user = gun.user();

function register() {
    user.create(username, password, (ack) => {
        if (ack.err) {
            console.error(ack.err);
        } else {
            console.log('User created successfully:', ack);
        }
    });
}

function login() {
    user.auth(username, password, (ack) => {
        if (ack.err) {
            console.error(ack.err);
        } else {
            console.log('User logged in successfully:', ack);
        }
    });
}
Enter fullscreen mode Exit fullscreen mode

I don’t think it can get more effortless than that! gun.js has a lot to offer, it is for most the convenient and the fastest tool i have used to create prototypes and scalable applications!

stay tuned for more!

originally published at by blog


Enter fullscreen mode Exit fullscreen mode

Top comments (0)