{"id":19528907,"url":"https://github.com/adonisjs/transmit-client","last_synced_at":"2025-04-26T11:33:45.371Z","repository":{"id":196260390,"uuid":"663570111","full_name":"adonisjs/transmit-client","owner":"adonisjs","description":"A client for the native Server-Sent-Event module of AdonisJS","archived":false,"fork":false,"pushed_at":"2024-04-24T11:33:30.000Z","size":53,"stargazers_count":17,"open_issues_count":1,"forks_count":1,"subscribers_count":4,"default_branch":"develop","last_synced_at":"2024-05-21T02:12:30.056Z","etag":null,"topics":[],"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/adonisjs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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},"funding":{"github":"thetutlage"}},"created_at":"2023-07-07T15:48:16.000Z","updated_at":"2024-04-26T08:32:17.000Z","dependencies_parsed_at":"2023-09-23T23:03:22.529Z","dependency_job_id":"c6ee3660-ff4f-42a1-9b45-beec5d1bd7d8","html_url":"https://github.com/adonisjs/transmit-client","commit_stats":null,"previous_names":["adonisjs/transmit-client"],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adonisjs%2Ftransmit-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adonisjs%2Ftransmit-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adonisjs%2Ftransmit-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adonisjs%2Ftransmit-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/adonisjs","download_url":"https://codeload.github.com/adonisjs/transmit-client/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224033028,"owners_count":17244520,"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-11-11T01:20:42.211Z","updated_at":"2024-11-11T01:20:43.101Z","avatar_url":"https://github.com/adonisjs.png","language":"TypeScript","funding_links":["https://github.com/sponsors/thetutlage"],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003ch1\u003e AdonisJS Transmit Client\u003c/h1\u003e\n  \u003cp\u003eA client for the native Server-Sent-Event (SSE) module of AdonisJS.\u003c/p\u003e\n\u003c/div\u003e\n\n\u003cbr /\u003e\n\n\u003cdiv align=\"center\"\u003e\n\n[![gh-workflow-image]][gh-workflow-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url] [![synk-image]][synk-url]\n\n\u003c/div\u003e\n\n\u003cdiv align=\"center\"\u003e\n  \u003ch3\u003e\n    \u003ca href=\"#installation\"\u003e\n      Usage\n    \u003c/a\u003e\n    \u003cspan\u003e | \u003c/span\u003e\n    \u003ca href=\"https://adonisjs.com\"\u003e\n      Checkout AdonisJS\n    \u003c/a\u003e\n  \u003c/h3\u003e\n\u003c/div\u003e\n\n\u003cbr /\u003e\n\n\u003chr /\u003e\n\nAdonisJS Transmit Client is a client for the native Server-Sent-Event (SSE) module of AdonisJS. It is built on top of the [EventSource](https://developer.mozilla.org/en-US/docs/Web/API/EventSource) API and provides a simple API to receive events from the server.\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n## Table of Contents\n\n  - [Installation](#installation)\n  - [Usage](#usage)\n  - [Creating a subscription](#creating-a-subscription)\n    - [Unsubscribing](#unsubscribing)\n    - [Subscription Request](#subscription-request)\n    - [Reconnecting](#reconnecting)\n- [Events](#events)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n## Installation\n\nInstall the package from the npm registry as follows:\n\n```sh\nnpm i @adonisjs/transmit-client\n```\n\n## Usage\n\nThe module exposes a `Transmit` class, which can be used to connect to the server and listen for events.\n\n```ts\nimport { Transmit } from '@adonisjs/transmit-client'\n\nconst transmit = new Transmit({\n  baseUrl: 'http://localhost:3333',\n})\n```\n\n## Creating a subscription\n\nThe `subscription` method is used to create a subscription to a channel. The method accepts the channel name\n\n```ts\nconst subscription = transmit.subscription('chat/1')\n```\n\nThen, you have to call the `create` method on the subscription to register it on the backend.\n\n```ts\nawait subscription.create()\n```\n\nYou can listen for events on the channel using the `onMessage` method. You can define as many listeners as you want on the same subscription.\n\n```ts\nsubscription.onMessage((message) =\u003e {\n  console.log(message)\n})\n```\n\nYou can also listen only once for a message using the `onMessagetOnce` method.\n\n```ts\nsubscription.onMessageOnce((message) =\u003e {\n  console.log('I will be called only once')\n})\n```\n\nNote listeners are local only; you can add them before or after registering your subscription on the server.\n\n### Unsubscribing\n\nThe `onMessage` method returns a function to remove the message handler from the subscription.\n\n```ts\nconst unsubscribe = subscription.onMessage(() =\u003e {\n  console.log('message received!')\n})\n\n// later\nunsubscribe()\n```\n\nIf you want to entirely remove the subscription from the server, you can call the `delete` method.\n\n```ts\nawait subscription.delete()\n```\n\n### Subscription Request\n\nYou can alter the subscription request by using the `beforeSubscribe` or `beforeUnsubscribe` options.\n\n```ts\nconst transmit = new Transmit({\n  baseUrl: 'http://localhost:3333',\n  beforeSubscribe: (_request: Request) =\u003e {\n    console.log('beforeSubscribe')\n  },\n  beforeUnsubscribe: (_request: Request) =\u003e {\n    console.log('beforeUnsubscribe')\n  },\n})\n```\n\n### Reconnecting\n\nThe transmit client will automatically reconnect to the server when the connection is lost. You can change the number of retries and hook into the reconnect lifecycle as follows:\n\n```ts\nconst transmit = new Transmit({\n  baseUrl: 'http://localhost:3333',\n  maxReconnectionAttempts: 5,\n  onReconnectAttempt: (attempt) =\u003e {\n    console.log('Reconnect attempt ' + attempt)\n  },\n  onReconnectFailed: () =\u003e {\n    console.log('Reconnect failed')\n  },\n})\n```\n\n# Events\n\nThe`Transmit` class uses the [`EventTarget`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget) class to emits multiple events.\n\n  ```ts\ntransmit.on('connected', () =\u003e {\n  console.log('connected')\n})\n\ntransmit.on('disconnected', () =\u003e {\n  console.log('disconnected')\n})\n\ntransmit.on('reconnecting', () =\u003e {\n  console.log('reconnecting')\n})\n```\n\n[gh-workflow-image]: https://img.shields.io/github/actions/workflow/status/adonisjs/transmit-client/test?style=for-the-badge\n[gh-workflow-url]: https://github.com/adonisjs/transmit-client/actions/workflows/test.yml \"Github action\"\n\n[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge\u0026logo=typescript\n[typescript-url]: \"typescript\"\n\n[npm-image]: https://img.shields.io/npm/v/@adonisjs/transmit-client.svg?style=for-the-badge\u0026logo=npm\n[npm-url]: https://npmjs.org/package/@adonisjs/transmit-client 'npm'\n\n[license-image]: https://img.shields.io/npm/l/@adonisjs/transmit-client?color=blueviolet\u0026style=for-the-badge\n[license-url]: LICENSE.md 'license'\n\n[synk-image]: https://img.shields.io/snyk/vulnerabilities/github/adonisjs/transmit-client?label=Synk%20Vulnerabilities\u0026style=for-the-badge\n[synk-url]: https://snyk.io/test/github/adonisjs/transmit-client?targetFile=package.json \"synk\"\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadonisjs%2Ftransmit-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadonisjs%2Ftransmit-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadonisjs%2Ftransmit-client/lists"}