{"id":22165591,"url":"https://github.com/textileio/go-libp2p-pubsub-rpc","last_synced_at":"2025-07-26T11:32:09.718Z","repository":{"id":39586846,"uuid":"388592633","full_name":"textileio/go-libp2p-pubsub-rpc","owner":"textileio","description":"RPC over libp2p pubsub with error handling","archived":false,"fork":false,"pushed_at":"2023-03-15T14:00:12.000Z","size":435,"stargazers_count":13,"open_issues_count":5,"forks_count":11,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-06-18T20:15:28.063Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/textileio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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":"2021-07-22T20:45:28.000Z","updated_at":"2023-11-17T20:27:17.000Z","dependencies_parsed_at":"2024-06-18T19:57:32.327Z","dependency_job_id":"3a23ddab-bfe9-4deb-9c7a-788a381681d7","html_url":"https://github.com/textileio/go-libp2p-pubsub-rpc","commit_stats":{"total_commits":25,"total_committers":4,"mean_commits":6.25,"dds":0.48,"last_synced_commit":"4f6d1168da75e2fa831c6e2913c4d1cba070d208"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/textileio%2Fgo-libp2p-pubsub-rpc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/textileio%2Fgo-libp2p-pubsub-rpc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/textileio%2Fgo-libp2p-pubsub-rpc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/textileio%2Fgo-libp2p-pubsub-rpc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/textileio","download_url":"https://codeload.github.com/textileio/go-libp2p-pubsub-rpc/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227673973,"owners_count":17802303,"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":[],"created_at":"2024-12-02T05:15:37.877Z","updated_at":"2024-12-02T05:15:38.635Z","avatar_url":"https://github.com/textileio.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-libp2p-pubsub-rpc\n\n[![Made by Textile](https://img.shields.io/badge/made%20by-Textile-informational.svg)](https://textile.io)\n[![Chat on Slack](https://img.shields.io/badge/slack-slack.textile.io-informational.svg)](https://slack.textile.io)\n[![standard-readme compliant](https://img.shields.io/badge/readme%20style-standard-brightgreen.svg)](https://github.com/RichardLitt/standard-readme)\n\n\u003e RPC over libp2p pubsub with error handling\n\n## Table of Contents\n\n- [Background](#background)\n- [Install](#install)\n- [Usage](#usage)\n- [Contributing](#contributing)\n- [Changelog](#changelog)\n- [License](#license)\n\n## Background\n\n`go-libp2p-pubsub-rpc` is an extension to [go-libp2p-pubsub](https://github.com/libp2p/go-libp2p-pubsub) that provides RPC-like functionality:\n\n- _Request/Response_ pattern with a peer\n- _Request/Multi-Response_ pattern with multiple peers\n\n## Install\n\n```bash\ngo get github.com/textileio/go-libp2p-pubsub-rpc\n```\n\n## Usage\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"fmt\"\n\t\"time\"\n\n\tcore \"github.com/libp2p/go-libp2p-core/peer\"\n\trpc \"github.com/textileio/go-libp2p-pubsub-rpc\"\n\t\"github.com/textileio/go-libp2p-pubsub-rpc/peer\"\n)\n\nfunc main() {\n\n\t//\n\t// PING PONG WITH TWO PEERS\n\t//\n\n\t// create a peer\n\tp1, _ := peer.New(peer.Config{\n\t\tRepoPath:   \"/tmp/repo1\",\n\t\tEnableMDNS: true, // can be used when peers are on the same network\n\t})\n\n\t// create another peer\n\tp2, _ := peer.New(peer.Config{\n\t\tRepoPath:   \"/tmp/repo2\",\n\t\tEnableMDNS: true,\n\t})\n\n\teventHandler := func(from core.ID, topic string, msg []byte) {\n\t\tfmt.Printf(\"%s event: %s %s\\n\", topic, from, msg)\n\t}\n\tmessageHandler := func(from core.ID, topic string, msg []byte) ([]byte, error) {\n\t\tfmt.Printf(\"%s message: %s %s\\n\", topic, from, msg)\n\t\tif string(msg) == \"ping\" {\n\t\t\treturn []byte(\"pong\"), nil\n\t\t} else {\n\t\t\treturn nil, errors.New(\"invalid request\")\n\t\t}\n\t}\n\n\tt1, _ := p1.NewTopic(context.Background(), \"mytopic\", true) // no need to subscribe if only publishing\n\tt1.SetEventHandler(eventHandler)                            // event handler reports topic membership events\n\tt1.SetMessageHandler(messageHandler)                        // message handle is any func that returns a response and error\n\n\tt2, _ := p2.NewTopic(context.Background(), \"mytopic\", true)\n\tt2.SetEventHandler(eventHandler)\n\tt2.SetMessageHandler(messageHandler) // using same message handler as peer1, but this could be anything\n\n\ttime.Sleep(time.Second) // wait for mdns discovery\n\n\t// peer1 requests \"pong\" from peer2\n\trc1, _ := t1.Publish(context.Background(), []byte(\"ping\"))\n\tr1 := \u003c-rc1\n\t// check r1.Err\n\tfmt.Printf(\"peer1 received \\\"%s\\\" from %s\\n\", r1.Data, r1.From)\n\n\t// peer2 requests \"pong\" from peer1\n\trc2, _ := t2.Publish(context.Background(), []byte(\"ping\"))\n\tr2 := \u003c-rc2\n\t// check r2.Err\n\tfmt.Printf(\"peer2 received \\\"%s\\\" from %s\\n\", r2.Data, r2.From)\n\n\t// peers can respond with an error\n\trc3, _ := t2.Publish(context.Background(), []byte(\"not a ping\"))\n\tr3 := \u003c-rc3\n\tfmt.Printf(\"peer2 received error \\\"%s\\\" from %s\\n\", r3.Err, r3.From)\n\n\t//\n\t// PING PONG WITH MULTIPLE PEERS\n\t//\n\n\t// create another peer\n\tp3, _ := peer.New(peer.Config{\n\t\tRepoPath:   \"/tmp/repo3\",\n\t\tEnableMDNS: true,\n\t})\n\tt3, _ := p3.NewTopic(context.Background(), \"mytopic\", true)\n\tt3.SetEventHandler(eventHandler)\n\tt3.SetMessageHandler(messageHandler)\n\n\ttime.Sleep(time.Second) // wait for mdns discovery\n\n\t// peer1 requests \"pong\" from peer2 and peer3\n\tctx, cancel := context.WithTimeout(context.Background(), time.Second)\n\tdefer cancel()\n\trc, _ := t1.Publish(ctx, []byte(\"ping\"), rpc.WithMultiResponse(true))\n\tfor r := range rc {\n\t\t// check r.Err\n\t\tfmt.Printf(\"peer1 received \\\"%s\\\" from %s\\n\", r.Data, r.From)\n\t}\n}\n```\n\n## Contributing\n\nPull requests and bug reports are very welcome ❤️\n\nThis repository falls under the Textile [Code of Conduct](./CODE_OF_CONDUCT.md).\n\nFeel free to get in touch by:\n-   [Opening an issue](https://github.com/textileio/go-libp2p-pubsub-rpc/issues/new)\n-   Joining the [public Slack channel](https://slack.textile.io/)\n-   Sending an email to contact@textile.io\n\n## Changelog\n\nA changelog is published along with each [release](https://github.com/textileio/go-libp2p-pubsub-rpc/releases).\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftextileio%2Fgo-libp2p-pubsub-rpc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftextileio%2Fgo-libp2p-pubsub-rpc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftextileio%2Fgo-libp2p-pubsub-rpc/lists"}