Git Product home page Git Product logo

transmit-client's Introduction

AdonisJS Transmit Client

A client for the native Server-Sent-Event (SSE) module of AdonisJS.


gh-workflow-image npm-image license-image synk-image



AdonisJS Transmit Client is a client for the native Server-Sent-Event (SSE) module of AdonisJS. It is built on top of the EventSource API and provides a simple API to receive events from the server.

Table of Contents

Installation

Install the package from the npm registry as follows:

npm i @adonisjs/transmit-client

# yarn
yarn add @adonisjs/transmit-client

# pnpm
pnpm add @adonisjs/transmit-client

Usage

The module exposes a Transmit class, which can be used to connect to the server and listen for events.

import { Transmit } from '@adonisjs/transmit-client'

const transmit = new Transmit({
  baseUrl: 'http://localhost:3333',
})

Subscribing to channels

The listenOn method accepts the channel name and a callback to be invoked when the event is received from the server.

transmit.listenOn<{ message: string }>('chat/1', (payload) => {
  console.log(payload.message)
})

You can also listen from a channel only once.

transmit.listenOnce<{ message: string }>('chat/1', () => {
  console.log('first message received!')
})

Subscription Request

You can alter the subscription request by using the beforeSubscribe or beforeUnsubscribe options.

const transmit = new Transmit({
  baseUrl: 'http://localhost:3333',
  beforeSubscribe: (_request: RequestInit) => {
    console.log('beforeSubscribe')
  },
  beforeUnsubscribe: (_request: RequestInit) => {
    console.log('beforeUnsubscribe')
  },
})

Reconnecting

The 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:

const transmit = new Transmit({
  baseUrl: 'http://localhost:3333',
  maxReconnectionAttempts: 5,
  onReconnectAttempt: (attempt) => {
    console.log('Reconnect attempt ' + attempt)
  },
  onReconnectFailed: () => {
    console.log('Reconnect failed')
  },
})

Unsubscribing

The listenOn method returns a function to unsubscribe from the channel.

const unsubscribe = transmit.listenOn('chat/1', () => {
  console.log('message received!')
})

// later
unsubscribe()

When unsubscribing from a channel, the client will remove the local listener for that channel. By default, it will not send a request to the server when there are no more listener to unsubscribe from the channel. You can change this behavior by setting the removeSubscriptionOnZeroListener option to true.

const transmit = new Transmit({
  baseUrl: 'http://localhost:3333',
  removeSubscriptionOnZeroListener: true,
})

You can also change the default settings locally by passing a boolean to the unsubscribe method.

const unsubscribe = transmit.listenOn('chat/1', () => {
  console.log('message received!')
})

// later
unsubscribe(true) // or false

Events

TheTransmit class extends the EventTarget class and emits multiple events.

transmit.on('connected', () => {
  console.log('connected')
})

transmit.on('disconnected', () => {
  console.log('disconnected')
})

transmit.on('reconnecting', () => {
  console.log('reconnecting')
})

transmit-client's People

Contributors

romainlanz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.