Skip to content

sivanzheng/reactive-realtime

Repository files navigation

reactive-realtime

An ReactiveX WebSocket Client SDK

Usage

  1. Create instance
import Realtime from 'reactive-realtime'

const client = new Realtime(<CONFIG>)
  1. Connect Server
client.connect(<TOKEN?>)
  1. Disconnect From Server
client.disconnect()

Example

run the following command:

parcel example/index.html

node example/server.js

Config

url: string                             // Server URL
protocol?: string | Array<string>       // The protocol to use to connect
renewAfterSeconds?: number              // Renew subscribe interval, default 60s
retryTimes?: number                     // Retry times, default 5
retryInterval?: number                  // Retry interval, default 5s
isRequiredAuth?: boolean                // Whether to require authentication, default true
messageQueueSize?: number               // Message queue size, default 1000
onOpen?: (data: unknown) => void        // Connection open callback
onClose?: (data: unknown) => void       // Connection close callback

Subscription

When you want to continuously subscribe to a stream

const feed = client.createFeed<SubscribedData>({
    <PARAMS>,
    <TOPIC>,
    <NAMESPACE>,
})

Subscribe

feed.subject.subscribe({ next: (data) => { } })

UnSubscribe

feed.unsubscribe()

Event

Watch

Listen for broadcast events

const watcher = client.watchEvent<ResponseDataType>(NAME, NAMESPACE, RESPOND)

watcher.subject.subscribe({ next: (data) => { }})

watcher.unsubscribe()

Send

Send a custom event, you can choose whether to wait for the response

// If waiting for a response
const response = await client.sendEvent<DataType, ResponseDataType>(<PARAMS>, true)

const { ack, error } = response

// If not

client.sendEvent<DataType>(<PARAMS>)

Request

When you need to send an request

Synchronous

const res = await client.requestSync<DataType, ResponseDataType>({
    <DATA>,
    <NAME>,
    <NAMESPACE>,
    <TIMEOUT>
})

Asynchronous

client.request<DataType, ResponseDataType>(
    <PARAMS>,
    <ON_RESPONSE>,
    <ON_ERROR>?,
)

About

An RxJS WebSocket client SDK

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published