Skip to content

Instantly share code, notes, and snippets.

@Lightnet
Created March 26, 2022 20:21
Show Gist options
  • Save Lightnet/817fbffbaf4a21d7a785772302b81984 to your computer and use it in GitHub Desktop.
Save Lightnet/817fbffbaf4a21d7a785772302b81984 to your computer and use it in GitHub Desktop.
vue 3.x gun plugin

The reason for the using Symbol is incase of matching variable for other plugins or class instance.

Note you need to config gun plugin the default config.

app.use(gunPlugin,{
  peers:['http://localhost:3000/gun']
  , localStorage: false
  , isDebug:true
});

Need to add some packages as buffer as it seem to be bug in vue set up. As well the window variable since it front end client.

If using SSR need be sure that window, document is config in some ways.

import { createApp } from 'vue'
import App from './App.vue';
app.use(gunPlugin,{
peers:['http://localhost:3000/gun']
, localStorage: false
, isDebug:true
});
const app = createApp(App);
app.use(gunPlugin);
app.mount('#app');
export const GunInjectKey = Symbol();
import Gun from "gun/gun"
import 'gun/lib/radix.js';
import 'gun/lib/radisk.js';
import 'gun/lib/store.js';
import 'gun/lib/rindexed.js';
import 'gun/lib/promise.js';
import SEA from 'gun/sea';
import { Buffer } from 'buffer'
if (typeof window !== "undefined"){
window.Buffer = Buffer;
window.setImmediate = setTimeout
}
import { GunInjectKey } from "./GunKeys.mjs";
export const gunPlugin = {
install(app, options) {
options = options || {
peers:['http://localhost:3000/gun'],
localStorage: false
}
const gun = Gun(options);
if(isDebug){// this for checking connection to gun server.
gun.on('hi', peer => {//peer connect
console.log('peer connect!');
});
gun.on('bye', (peer)=>{// peer disconnect
console.log('Disconnected from peer!');
});
}
app.provide(GunInjectKey, gun) // set instance in vue
}
}
export default gunPlugin;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment