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

7.6.0 • 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
7.6.046latest
7.6.1-f7cc558.09nightly
6.20.1-621b475.04dev/20
6.9.1-fa5ac34.02dev/5
6.34.1-b036e5d.02dev/63
6.32.3-89ca939.02dev/56
6.32.2-3476a95.02dev/53
6.28.2-9ac11c6.02dev/37
6.24.2-7ddc891.02dev/29
6.23.1-d5c2fe6.02dev/25
6.7.1-4e6b4a3.01dev/1
6.35.3-e008e09.01dev/67
6.32.5-2629e79.01dev/58
6.29.3-fe4e045.01dev/42
6.27.1-f6a08a6.01dev/23
6.18.1-a203325.01dev/17
6.11.2-61f4f8b.01dev/7

Version History

VersionDownloads (Last 7 Days)Published
7.6.1-f7cc558.09
7.6.046
7.5.4-a561051.010
7.5.36
7.5.3-2c40726.05
7.5.2-94bff6e.04
7.5.214
7.5.2-d68b072.01
7.5.11
7.5.1-f996b3e.00
7.5.013
7.4.1-9a54e83.00
7.4.00
7.3.1-1cd86e2.02
7.3.02
7.1.1-0e753e3.01
7.2.02
7.1.02
7.0.3-ae47dd5.01
7.0.3-d7a9877.01
7.0.21
7.0.2-3cbb4fc.02
7.0.116
7.0.1-91b5585.02
7.0.02
6.36.1-938a6a9.02
6.36.027
6.35.3-7ce3db4.02
6.35.3-2859517.02
6.35.3-e008e09.01
6.35.3-ab24b11.02
6.35.3-38f774d.03
6.35.21
6.35.2-412ed8b.01
6.35.11
6.35.01
6.34.1-220041d.02
6.34.1-b036e5d.02
6.34.1-6ae6435.01
6.34.01
6.32.7-686c44c.02
6.33.13
6.33.017
6.32.7-289cad7.02
6.32.62
6.32.6-35556b2.01
6.32.53
6.32.5-2629e79.01
6.32.5-4306d45.02
6.32.45
6.32.3-89ca939.02
6.32.4-a4e315c.01
6.32.32
6.32.3-a30f54f.02
6.32.2-731162e.01
6.32.2-3476a95.02
6.32.21
6.32.2-da6d23d.01
6.32.11
6.31.2-5b810e5.01
6.32.04
6.31.2-bb9f6a4.03
6.31.116
6.31.1-7ca0115.01
6.31.02
6.30.5-d50bb81.01
6.30.42
6.30.4-48268a5.02
6.30.37
6.30.3-cae094b.02
6.30.21
6.30.2-bc5ef81.03
6.30.11
6.30.1-4204057.02
6.30.02
6.29.3-fe4e045.01
6.29.3-669217e.03
6.29.22
6.29.2-310dd78.02
6.29.12
6.28.4-164cc5b.03
6.29.02
6.28.31
6.28.3-7c09769.03
6.28.3-54dff84.01
6.28.2-9ac11c6.02
6.28.22
6.28.2-fbfd068.01
6.28.11
6.28.1-09c5133.02
6.27.1-f6a08a6.01
6.27.1-3065512.01
6.28.02
6.27.1-be8d01c.01
6.26.4-b4d0c1f.02
6.27.01
6.26.4-2eb3673.02
6.26.31
6.26.3-3e5fb0d.02
6.26.22
6.26.12
6.26.1-4e1acd6.01
6.26.1-c3b0164.02
6.26.012
6.25.2-343f6e2.02
6.25.2-843ba7e.01
6.24.2-dc02f91.02
6.22.1-bc84d0c.01
6.22.1-b0a1854.03
6.25.12
6.25.01
6.24.2-18b8e6d.02
6.24.2-7ddc891.02
6.24.2-6f5059f.02
6.24.2-d89a258.03
6.24.11
6.24.1-13ac472.01
6.23.1-98c3335.02
6.23.1-d5c2fe6.02
6.24.01
6.22.1-600ab29.02
6.22.1-cbca8f1.02
6.22.1-431d613.02
6.23.1-d8b4c4c.02
6.22.1-3033cb6.01
6.22.1-54cd206.01
6.22.1-12d640a.02
6.23.026
6.22.02
6.21.12
6.21.1-265b0a3.02
6.21.1-8cbe3b5.02
6.21.01
6.20.2-c7b567d.01
6.20.11
6.20.01
6.20.1-621b475.04
6.20.1-273737d.02
6.18.1-4042f17.01
6.18.1-a203325.01
6.19.05
6.18.1-b6ce8d0.01
6.18.03
6.17.4-bbdf1ed.01
6.17.31
6.16.1-2823f7b.02
6.17.01
6.16.02
6.15.1-fee6c8f.01
6.15.038
6.14.2-5d7d534.03
6.14.11
6.14.02
6.13.3-acef8cc.01
6.13.22
6.13.01
6.12.2-b78b0a5.02
6.12.12
6.12.1-78a259e.01
6.12.02
6.11.2-27cbf6f.01
6.11.12
6.11.2-61f4f8b.01
6.11.1-cc23ab2.01
6.10.1-958b188.01
6.10.01
6.9.1-e6a3d26.02
6.9.1-fa5ac34.02
6.9.05
6.8.3-6df654f.01
6.8.2-d546836.01
6.8.2-a982330.02
6.8.1-0841e8b.01
6.8.02
6.7.1-b2e0776.02
6.7.1-4e6b4a3.01
6.7.1-070e5e4.01
6.7.1-cafe37a.02
6.7.1-8bea649.02
6.7.1-e8866e9.02
6.7.1-80c1951.01

Package Sidebar

Install

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

Weekly Downloads

545

Version

7.6.0

License

CC-BY-ND-4.0

Unpacked Size

6.79 MB

Total Files

3233

Last publish

Collaborators

  • ekoapp
  • touchaponk
  • pakkawat