How to use the gun.on function in gun

To help you get started, we’ve selected a few gun examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github zrrrzzt / gun-restrict-examples / gun-servers / server-gun-restricted-put.js View on Github external
const port = 8000
const Gun = require('gun')

function hasValidToken (msg) {
  return msg && msg && msg.headers && msg.headers.token && msg.headers.token === 'thisIsTheTokenForReals'
}

// Add listener
Gun.on('opt', function (ctx) {
  if (ctx.once) {
    return
  }
  // Check all incoming traffic
  ctx.on('in', function (msg) {
    var to = this.to
    // restrict put
    if (msg.put) {
      if (hasValidToken(msg)) {
        console.log('writing')
        to.next(msg)
      } else {
        console.log('not writing')
      }
    } else {
      to.next(msg)
github zrrrzzt / gun-restrict-examples / gun-servers / server-gun-restricted.js View on Github external
const port = 8000
const Gun = require('gun')

function hasValidToken (msg) {
  return msg && msg.headers && msg.headers.token && msg.headers.token === 'thisIsTheTokenForReals'
}

// Add listener
Gun.on('opt', function (ctx) {
  if (ctx.once) {
    return
  }
  // Check all incoming traffic
  ctx.on('in', function (msg) {
    var to = this.to
    // restrict everything with invalid tokens
    if (msg.put || msg.get) {
      if (hasValidToken(msg)) {
        console.log('okay')
        to.next(msg)
      } else {
        console.log('not okay')
        console.log(msg)
      }
    } else {
github SocialXNetwork / socialx_react_native / packages / database / src / tcpServer.ts View on Github external
import ws from 'ws';

const WebSocketServer = ws.Server;

const port: string | number =
	process.env.OPENSHIFT_NODEJS_PORT ||
	process.env.VCAP_APP_PORT ||
	process.env.PORT ||
	process.argv[2] ||
	8765;

//
// have to do this before instancing gun(?)
const gunPeers: Array<{ send: (msg: any) => void }> = [];

Gun.on('out', function(msg: any) {
	// @ts-ignore
	this.to.next(msg);
	msg = JSON.stringify(msg);
	// tslint:disable-next-line
	gunPeers.forEach(function(peer) {
		peer.send(msg);
	});
});

const gun = Gun({
	file: 'data',
});

const server = http.createServer((req, res) => {
	let insert = '';
	if (req.url!.endsWith('gun.js')) {
github amark / gun / test / axe / holy-grail.js View on Github external
tests.push(client.run(function(test){
				localStorage.clear(); console.log('Clear localStorage!!!');
				var env = test.props;
				var opt = {peers:['http://'+ env.config.IP + ':' + (env.config.port + 1) + '/gun'], wait: 1000};
				var pid = location.hash.slice(1);
				if (pid) { opt.pid = pid; }
				Gun.on('opt', function(ctx) {
					this.to.next(ctx);
					ctx.on('hi', function(opt) {
						document.getElementById('pid').innerHTML = (document.getElementById('pid').innerHTML || "-")  + ', ' + this.on.opt.pid;
					});
				});
				var gun = window.gun = Gun(opt);
				window.ref = gun.get('holy').get('grail');
			}, {i: i += 1, config: config}));
		});

gun

A realtime, decentralized, offline-first, graph data synchronization engine.

(Zlib OR MIT OR Apache-2.0)
Latest version published 13 days ago

Package Health Score

83 / 100
Full package analysis