{"id":20388386,"url":"https://github.com/moonshrd/p2chat","last_synced_at":"2025-04-12T10:38:05.144Z","repository":{"id":114106889,"uuid":"175601395","full_name":"MoonSHRD/p2chat","owner":"MoonSHRD","description":"local communication core","archived":false,"fork":false,"pushed_at":"2024-09-23T14:09:22.000Z","size":283,"stargazers_count":6,"open_issues_count":9,"forks_count":2,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-26T05:33:12.504Z","etag":null,"topics":["iot","libp2p","mdns","mesh","peerdiscovery","pubsub"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MoonSHRD.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-03-14T10:39:16.000Z","updated_at":"2025-02-10T23:59:48.000Z","dependencies_parsed_at":"2024-06-20T05:55:39.812Z","dependency_job_id":"8c3338a4-e0e9-4ba8-bdd2-d6df658b4f31","html_url":"https://github.com/MoonSHRD/p2chat","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MoonSHRD%2Fp2chat","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MoonSHRD%2Fp2chat/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MoonSHRD%2Fp2chat/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MoonSHRD%2Fp2chat/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MoonSHRD","download_url":"https://codeload.github.com/MoonSHRD/p2chat/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248555533,"owners_count":21123923,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["iot","libp2p","mdns","mesh","peerdiscovery","pubsub"],"created_at":"2024-11-15T03:09:36.693Z","updated_at":"2025-04-12T10:38:05.118Z","avatar_url":"https://github.com/MoonSHRD.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# P2Chat\nP2Chat - is a core local messenger library, which based on Libp2p stack.\n\nP2Chat basicaly supports discovery through **mDNS** service and support messaging via **PubSub**\n\nIt supports following features:\n- devices autodiscovery by `Rendezvous string`\n- topic list exchanging between peers\n- autoconnect group chats by `PubSub`\n- default signing and validating messages (crypto)\n- crossplatform\n\n## How it works?\n\n```\n  cmd/main.go - main logic\n  pkg/mdns.go - mdns logic\n  api/protocol.go - protocol logic (message struct and handle)\n```\n\n### Example work scenario\n\n### Step 1 - establising a network\n\nAs first step we want to discover every our peer in network and connect to them.\n\nFirst thing we need to do is parse configuration of ourselves:\n\n` sourceMultiAddr, _ := multiaddr.NewMultiaddr(fmt.Sprintf(\"/ip4/%s/tcp/%d\", cfg.listenHost, cfg.listenPort)) `\n\nmultiaddress is universal address of our host, based on our IP address and host, it is basic IPFS identity\nif IP is not known, then we could use `0.0.0.0` as a default wildcard for _ourselves_\n\n\n\nThen we create `libp2p` host object:\n\n`host, err := libp2p.New(\n\t\tctx,\n\t\tlibp2p.ListenAddrs(sourceMultiAddr),\n\t\tlibp2p.Identity(prvKey),\n\t)\n`\n\nAfter we have been created a host, we could start __peerdiscovery__ using __mDNS__\nThis is not the only way to peerdiscovery mechanism; alternatively we could use DHT or rendez-vous point as we wish.\n\n`peerChan := pkg.InitMDNS(ctx, host, cfg.RendezvousString) `\n\nEach time we discover a new peer in serviceTopic, we add it to a local peerstore and _connect_ to it:\n\n```\ncase newPeer := \u003c-peerChan:\n\t\t\t{\n\t\t\t\tfmt.Println(\"\\nFound peer:\", newPeer, \", add address to peerstore\")\n\n\t\t\t\t// Adding peer addresses to local peerstore\n\t\t\t\thost.Peerstore().AddAddr(newPeer.ID, newPeer.Addrs[0], peerstore.PermanentAddrTTL)\n\t\t\t\t// Connect to the peer\n\t\t\t\tif err := host.Connect(ctx, newPeer); err != nil {\n\t\t\t\t\tfmt.Println(\"Connection failed:\", err)\n\t\t\t\t}\n\t\t\t\tfmt.Println(\"Connected to:\", newPeer)\n\t\t\t\tfmt.Println(\"\u003e \")\n\t\t\t}\n```\n\nThis far we get every moonshard devices discoverable and connected into one network\n\n### Step 2 - setting up PubSub (publish/subscribe) and discover network topics\n\nSo, we have swarm of our peers connected to each other this far. \nHowever, we may want to _separate_ peers into groups, based on their _topics_. \nIf we will __not__ do such thing - we will face with problem of broadcast - each time when we send message to the network - we send it to __all__ peers in network, so it may be working __slowly__ when we have to much peers.\n\nFirst thing to do is initialize PubSub object as \n```\npb, err := pubsub.NewFloodsubWithProtocols(context.Background(), host, []protocol.ID{protocol.ID(cfg.ProtocolID)}, pubsub.WithMessageSigning(true), pubsub.WithStrictSignatureVerification(true))\n```\nthis line initialize pubsub, using floodsub protocol (alternatively we could use gossipsub instead of), our libp2p host configuration from previous step, enable message signing and signature verification.\n\nThen, we could easily subscribe to any topic using `pb.Subcribe(topic)` as :\n```\nsubscription, err := pb.Subscribe(cfg.RendezvousString)\nserviceTopic = cfg.RendezvousString\n```\nservice topic - is a main general topic, which group _every_ peer in a network, so we could use it for pushing some service and important information.\n\nAfter we did this - we want to know about _other_ topics in our network, so we could subscribe to them as well. \nFor doing so - we send some service message to service topic (which means that we are asking every peer in network about their topics) as:\n```\ngo readSub(subscription, incomingMessages)\ngo getNetworkTopics()\n```\n\nAfter that we could easily subscribe, publish and create new topics using such functions as \n` newTopic(), writeTopic(), readSub() `\n\n\n\n## Building\nRequire go version \u003e=1.12 , so make sure your `go version` is okay.  \n**WARNING!** Building happen only when this project locates outside of GOPATH environment.\n\n```bash\n$ git clone https://github.com/MoonSHRD/p2chat\n$ cd p2chat\n$ go mod tidy\n$ make\n```\n\nIf you have trouble with go mod, then you can try clean source building\n```\n$ go get -v -d ./... # not sure that it's neccessary\n```\nBuilded binary will be in `./cmd/`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoonshrd%2Fp2chat","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmoonshrd%2Fp2chat","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoonshrd%2Fp2chat/lists"}