@amityco/ts-sdk-react-native
TypeScript icon, indicating that this package has built-in type declarations

6.32.4 • Public • Published

Amity Social Cloud Typescript SDK for React-Native

Getting Started

  • With npm: npm install @amityco/ts-sdk-react-native
  • With yarn: yarn add @amityco/ts-sdk-react-native

Runtime support

  • Modern browsers (es2017 compliant)
  • NodeJS (via esm imports)
  • React-Native

Creating a client instance

To connect your users to our platform, you need to create a client instance and connect it.

import { Client } from '@amityco/ts-sdk-react-native';

(async () => {
  const sessionHandler: Amity.SessionHandler = {
    sessionWillRenewAccessToken(renewal: Amity.Renewal) {
      renewal.renew();
      /*
       * If using an auth token
       *
       * try {
       *  renew.renewWithAuthToken(authToken)
       * } catch() {
       *  sdk will try to renew again at a later time
       *
       *  renew.unableToRetrieveAuthToken()
       * }
       */
    },
  };
  const client = Client.createClient(API_KEY, API_REGION);

  let isConnected = await Client.login({ userId: USER_ID }, sessionHandler);
})();

The API_KEY and API_REGION are given at the time you create an application in the Amity Portal.

You can listen to disconnection by using the onClientDisconnected function:

import { Client } from '@amityco/ts-sdk-react-native';

const unsub = Client.onClientDisconnected(() => (isConnected = false));

The unsub's return value is a dispose function which is used to clean the memory of your client's application. It's been designed to fit perfectly with React, in a useEffect such as:

useEffect(() => onClientConnected(() => setConnected(false)), [])

## Going further

### Domain types

All the necessary types are automatically exposed in the `Amity` namespace. As long as you import the SDK once, the namespace will be available in your workspace. You can start to type `Amity.` and see the list of types in your intellisense after having imported the SDK in any file of your project.

### Subscribing to real time events

We expose a couple of functions for your to receive simply the events that could fire from our servers, for example, if another user would change their display name, or if a channel would receive a new message.

The `subscribeTopic` method retuns an unsubscriber that can be called on clean up to stop recieving further events.

```ts
import { subscribeTopic, getUserTopic } from '@amityco/ts-sdk-react-native'

const unsub = subscribeTopic(getUserTopic({} as Amity.User))

Observe an object using Live Object

For an 'all-in-one' approach, we expose live objects, which allow to fetch an object and register to its changes all at once. This has been designed to fit with React's useState and useEffect:

import { UserRepository } from '@amityco/ts-sdk-react-native'

const Component = ({ userId }: { userId: string }) => {
  const [user, setUser] = useState<Amity.User>({})

  useEffect(() => UserRepository.getUser(userId, ({ data: user, loading, error }) => {
    // ensure you call unsub() on cleanup or unmount
    const unsub = subscribeTopic(getUserTopic(user))

    setUser(user)
  }), [userId])

  return <div>{JSON.stringify(user, null, 2)}
}

Observe a collection using Live Collection

Similar to the concept of live object, we expose live collection, which allow to fetch a collection and register to its changes all at once. This has been designed to fit with React's useState and useEffect:

import { UserRepository.getUsers } from '@amityco/ts-sdk-react-native'

const Component = () => {
  const [users, setUsers] = useState<Amity.User[]>({})

  useEffect(() => UserRepository.getUsers({}, ({ data, loading, error, nextPage, hasNextPage }) => {
    // ensure you call unsub() on cleanup or unmount
    // Subscribe to each user in collection for getting real time updates

    setUsers(users)
  }), [userId])

  return <div>{JSON.stringify(user, null, 2)}
}

Documentation

To see the comprehensive documentation please visit our documentation page.

If you have any questions, you can visit our forum.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
6.7.1-4e6b4a3.01dev/1
6.9.1-fa5ac34.00dev/5
6.28.2-9ac11c6.00dev/37
6.27.1-f6a08a6.00dev/23
6.24.2-7ddc891.00dev/29
6.23.1-d5c2fe6.00dev/25
6.18.1-a203325.00dev/17
6.11.2-61f4f8b.00dev/7
6.32.488latest
6.32.3-89ca939.049dev/56
6.32.4-a4e315c.04nightly
6.32.2-3476a95.01dev/53
6.29.3-fe4e045.01dev/42
6.20.1-621b475.01dev/20

Version History

VersionDownloads (Last 7 Days)Published
6.32.488
6.32.3-89ca939.049
6.32.4-a4e315c.04
6.32.363
6.32.3-a30f54f.02
6.32.2-731162e.01
6.32.2-3476a95.01
6.32.212
6.32.2-da6d23d.02
6.32.12
6.31.2-5b810e5.03
6.32.02
6.31.2-bb9f6a4.01
6.31.1143
6.31.1-7ca0115.01
6.31.01
6.30.5-d50bb81.01
6.30.40
6.30.4-48268a5.00
6.30.34
6.30.3-cae094b.01
6.30.22
6.30.2-bc5ef81.01
6.30.12
6.30.1-4204057.00
6.30.00
6.29.3-fe4e045.01
6.29.3-669217e.00
6.29.20
6.29.2-310dd78.00
6.29.117
6.28.4-164cc5b.01
6.29.01
6.28.31
6.28.3-7c09769.02
6.28.3-54dff84.00
6.28.2-9ac11c6.00
6.28.20
6.28.2-fbfd068.01
6.28.11
6.28.1-09c5133.01
6.27.1-f6a08a6.00
6.27.1-3065512.00
6.28.00
6.27.1-be8d01c.00
6.26.4-b4d0c1f.01
6.27.00
6.26.4-2eb3673.00
6.26.31
6.26.3-3e5fb0d.01
6.26.21
6.26.11
6.26.1-4e1acd6.00
6.26.1-c3b0164.00
6.26.03
6.25.2-343f6e2.00
6.25.2-843ba7e.01
6.24.2-dc02f91.01
6.22.1-bc84d0c.00
6.22.1-b0a1854.01
6.25.11
6.25.01
6.24.2-18b8e6d.02
6.24.2-7ddc891.00
6.24.2-6f5059f.00
6.24.2-d89a258.00
6.24.11
6.24.1-13ac472.01
6.23.1-98c3335.00
6.23.1-d5c2fe6.00
6.24.02
6.22.1-600ab29.00
6.22.1-cbca8f1.01
6.22.1-431d613.01
6.23.1-d8b4c4c.01
6.22.1-3033cb6.01
6.22.1-54cd206.00
6.22.1-12d640a.01
6.23.013
6.22.03
6.21.11
6.21.1-265b0a3.00
6.21.1-8cbe3b5.00
6.21.01
6.20.2-c7b567d.00
6.20.11
6.20.01
6.20.1-621b475.01
6.20.1-273737d.00
6.18.1-4042f17.01
6.18.1-a203325.00
6.19.02
6.18.1-b6ce8d0.00
6.18.02
6.17.4-bbdf1ed.00
6.17.32
6.16.1-2823f7b.01
6.17.01
6.16.01
6.15.1-fee6c8f.00
6.15.022
6.14.2-5d7d534.01
6.14.11
6.14.02
6.13.3-acef8cc.00
6.13.21
6.13.01
6.12.2-b78b0a5.00
6.12.11
6.12.1-78a259e.01
6.12.01
6.11.2-27cbf6f.00
6.11.11
6.11.2-61f4f8b.00
6.11.1-cc23ab2.01
6.10.1-958b188.02
6.10.02
6.9.1-e6a3d26.00
6.9.1-fa5ac34.00
6.9.01
6.8.3-6df654f.00
6.8.2-d546836.00
6.8.2-a982330.00
6.8.1-0841e8b.00
6.8.01
6.7.1-b2e0776.00
6.7.1-4e6b4a3.01
6.7.1-070e5e4.03
6.7.1-cafe37a.02
6.7.1-8bea649.02
6.7.1-e8866e9.02
6.7.1-80c1951.02

Package Sidebar

Install

npm i @amityco/ts-sdk-react-native

Weekly Downloads

518

Version

6.32.4

License

CC-BY-ND-4.0

Unpacked Size

6.43 MB

Total Files

3047

Last publish

Collaborators

  • ekoapp
  • touchaponk
  • pakkawat