0.0.1 • Published 3 years ago

@space-kit/redis-realtime-react v0.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

redis-realtime-react

Redis realtime react client

Installation

npm install @space-kit/redis-realtime-react

Usage

Provider

Add the RealtimeProvider to the top level with db name and base url.

import React from 'react'
import { RealtimeProvider } from '@space-kit/redis-realtime-react'
import App from './app'

function Root() {
  return (
    <Container>
      <RealtimeProvider baseUrl="localhost:5000" db="todos" secure={false}>
        <App />
      </RealtimeProvider>
    </Container>
  )
}
PropsDescriptionDefault
baseUrlBase url of the domain where backend is hosted (without protocols)nil
dbDatabase name that is to be connectednil
secureWhether the web socket connection is securetrue

Hooks

useRealtime

import React from 'react'
import { useRealtime } from '@space-kit/redis-realtime-react'

function App() {
  const { publisher, subscribe } = useRealtime()
  const { setDb } = publisher('text')
  const textValue = subscribe('text')

  const onChange = (e) => {
    setDb(e.target.value)
  }

  return (
    <input value={textValue} placeholder="Type something here" onChange={onChange} />
  )
}
ParametersDescription
publisherCreate a publish for a key in db
subscribeCreate a subscriber for a key in db
Publisher Parameters
ParametersDescription
setDbSimilar to set json in redis it sets the value of publisher key
Subscriber Parameters
ParametersDescription
dataValue of the key in redis db
isLoadingWhether the value is being loaded