{"id":17677410,"url":"https://github.com/unadlib/use-transport","last_synced_at":"2026-01-02T21:31:55.959Z","repository":{"id":230738551,"uuid":"779359845","full_name":"unadlib/use-transport","owner":"unadlib","description":"A React hook with simple and responsible universal transports","archived":false,"fork":false,"pushed_at":"2024-09-11T18:29:05.000Z","size":225,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-10-24T22:17:09.314Z","etag":null,"topics":["broadcast","data-transport","electron","iframe","react","react-hooks","service-worker","shared-worker","webrtc","webworker"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/unadlib.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2024-03-29T16:50:38.000Z","updated_at":"2024-09-11T18:24:33.000Z","dependencies_parsed_at":null,"dependency_job_id":"1e9cd698-f443-4733-aa10-b42c970ec426","html_url":"https://github.com/unadlib/use-transport","commit_stats":{"total_commits":11,"total_committers":1,"mean_commits":11.0,"dds":0.0,"last_synced_commit":"49f1625582cec849eab014614a2246e6dae2928b"},"previous_names":["unadlib/use-transport"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unadlib%2Fuse-transport","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unadlib%2Fuse-transport/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unadlib%2Fuse-transport/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unadlib%2Fuse-transport/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/unadlib","download_url":"https://codeload.github.com/unadlib/use-transport/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243208737,"owners_count":20254092,"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":["broadcast","data-transport","electron","iframe","react","react-hooks","service-worker","shared-worker","webrtc","webworker"],"created_at":"2024-10-24T07:28:37.092Z","updated_at":"2026-01-02T21:31:55.862Z","avatar_url":"https://github.com/unadlib.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# use-transport\n\n![Node CI](https://github.com/unadlib/use-transport/workflows/Node%20CI/badge.svg)\n[![npm](https://img.shields.io/npm/v/use-transport.svg)](https://www.npmjs.com/package/use-transport)\n![license](https://img.shields.io/npm/l/use-transport)\n\nA React hook with simple and responsible universal transports.\n\n### Motivation\n\n`use-transport` is a React hook that provides a simple and responsible way to manage data transport. It is designed to be used with [`data-transport`](https://github.com/unadlib/data-transport) to provide a universal transport solution.\n\n`data-transport` is a generic and responsible communication transporter:\n\n- iframe\n- Broadcast\n- Web Worker\n- Service Worker\n- Shared Worker\n- Browser Extension\n- Node.js\n- WebRTC\n- Electron\n- More transport port\n\n### Installation\n\n```bash\nnpm install use-transport data-transport\n# or\nyarn add use-transport data-transport\n```\n\n### Features\n\n- Simple and responsible\n- Support for multiple transport ports\n- Support for mock transport\n- Full TypeScript support\n\n### API\n\nYou can use the `use-transport` hook to create a transport instance. And then use the `emit` and `listen` methods to send and receive messages.\n\n```jsx\nimport React from 'react';\nimport { useTransport } from 'use-transport';\n\nconst App = () =\u003e {\n  const transport = useTransport('IFrameMain', {});\n\n  transport.listen(\n    'hello',\n    async () =\u003e {\n      return 'world';\n    },\n    []\n  );\n\n  const handleClick = async () =\u003e {\n    const response = await transport.emit('ping');\n    console.log(response);\n  };\n\n  return \u003cbutton onClick={handleClick}\u003ePing\u003c/button\u003e;\n};\n```\n\n#### Parameters\n\n| Parameter | Type   | Description            |\n| --------- | ------ | ---------------------- |\n| `type`    | enums  | Transport port type    |\n| `options` | object | Transport port options |\n\n#### Returns\n\n| Return             | Type                                                              | Description          |\n| ------------------ | ----------------------------------------------------------------- | -------------------- |\n| `transport.emit`   | (name: string \\| options, ...args: any[]) =\u003e any                  | Emit a message       |\n| `transport.listen` | (name: string, fn: (...args: any[]) =\u003e any, deps?: any[]) =\u003e void | Listen for a message |\n\n\u003e The `use-transport` hook returns a transport instance. more API details can be found in the [data-transport](https://github.com/unadlib/data-transport) documentation.\n\n\u003e If you want to use the `use-transport` hook with TypeScript, you need to use `Transport` type from `use-transport`.\n\n```tsx\ntype Interaction = {\n  listen: {\n    foo: (value: number) =\u003e Promise\u003cnumber\u003e;\n  };\n  emit: {\n    bar: (value: number) =\u003e Promise\u003cvoid\u003e;\n  };\n};\n\nconst transport: Transport\u003cInteraction\u003e = useTransport('IFrameMain', {});\n```\n\n## License\n\n`use-transport` is [MIT licensed](https://github.com/unadlib/use-transport/blob/main/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Funadlib%2Fuse-transport","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Funadlib%2Fuse-transport","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Funadlib%2Fuse-transport/lists"}