An ReactiveX WebSocket Client SDK
- Create instance
import Realtime from 'reactive-realtime' const client = new Realtime(<CONFIG>)
- Connect Server
client.connect(<TOKEN?>)
- Disconnect From Server
client.disconnect()
run the following command:
parcel example/index.html
node example/server.js
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
When you want to continuously subscribe to a stream
const feed = client.createFeed<SubscribedData>({
<PARAMS>,
<TOPIC>,
<NAMESPACE>,
})
feed.subject.subscribe({ next: (data) => { } })
feed.unsubscribe()
Listen for broadcast events
const watcher = client.watchEvent<ResponseDataType>(NAME, NAMESPACE, RESPOND)
watcher.subject.subscribe({ next: (data) => { }})
watcher.unsubscribe()
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>)
When you need to send an request
const res = await client.requestSync<DataType, ResponseDataType>({
<DATA>,
<NAME>,
<NAMESPACE>,
<TIMEOUT>
})
client.request<DataType, ResponseDataType>(
<PARAMS>,
<ON_RESPONSE>,
<ON_ERROR>?,
)