6.32.0 • Published 8 months ago

@amityco/ts-sdk-react-native v6.32.0

Weekly downloads
-
License
CC-BY-ND-4.0
Repository
-
Last release
8 months ago

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.

6.32.0

8 months ago

6.31.2-bb9f6a4.0

8 months ago

6.31.1

8 months ago

6.30.5-d50bb81.0

8 months ago

6.31.1-7ca0115.0

8 months ago

6.30.4-48268a5.0

9 months ago

6.31.0

8 months ago

6.30.3

9 months ago

6.30.4

8 months ago

6.30.3-cae094b.0

9 months ago

6.30.1

9 months ago

6.30.2

9 months ago

6.30.0

10 months ago

6.30.1-4204057.0

9 months ago

6.30.2-bc5ef81.0

9 months ago

6.29.2-310dd78.0

11 months ago

6.29.3-fe4e045.0

10 months ago

6.27.1-f6a08a6.0

11 months ago

6.27.1-3065512.0

11 months ago

6.29.1

11 months ago

6.29.2

10 months ago

6.29.0

11 months ago

6.27.0

12 months ago

6.26.4-2eb3673.0

12 months ago

6.28.2-9ac11c6.0

11 months ago

6.28.4-164cc5b.0

11 months ago

6.28.3-7c09769.0

11 months ago

6.26.4-b4d0c1f.0

11 months ago

6.28.2

11 months ago

6.28.3

11 months ago

6.28.0

11 months ago

6.28.1

11 months ago

6.26.0

1 year ago

6.26.1

1 year ago

6.28.3-54dff84.0

11 months ago

6.28.2-fbfd068.0

11 months ago

6.27.1-be8d01c.0

11 months ago

6.29.3-669217e.0

10 months ago

6.25.1

1 year ago

6.25.0

1 year ago

6.28.1-09c5133.0

11 months ago

6.26.2

1 year ago

6.26.3

1 year ago

6.24.1

1 year ago

6.24.0

1 year ago

6.23.0

1 year ago

6.22.0

1 year ago

6.21.1

1 year ago

6.21.0

1 year ago

6.20.1

1 year ago

6.20.0

1 year ago

6.19.0

1 year ago

6.18.0

1 year ago

6.17.3

1 year ago

6.17.0

1 year ago

6.16.0

1 year ago

6.15.0

1 year ago

6.14.1

2 years ago

6.13.2

2 years ago

6.14.0

2 years ago

6.13.3-acef8cc.0

2 years ago

6.12.2-b78b0a5.0

2 years ago

6.12.1-78a259e.0

2 years ago

6.13.0

2 years ago

6.12.1

2 years ago

6.12.0

2 years ago

6.11.2-27cbf6f.0

2 years ago

6.11.1

2 years ago

6.11.2-61f4f8b.0

2 years ago

6.11.1-cc23ab2.0

2 years ago

6.10.1-958b188.0

2 years ago

6.10.0

2 years ago

6.9.1-e6a3d26.0

2 years ago

6.9.1-fa5ac34.0

2 years ago

6.9.0

2 years ago

6.8.3-6df654f.0

2 years ago

6.8.2-d546836.0

2 years ago

6.8.2-a982330.0

2 years ago

6.8.1-0841e8b.0

2 years ago

6.8.0

2 years ago

6.7.1-b2e0776.0

2 years ago

6.7.1-4e6b4a3.0

2 years ago

6.7.1-070e5e4.0

2 years ago

6.7.1-cafe37a.0

2 years ago

6.7.1-8bea649.0

2 years ago

6.7.1-e8866e9.0

2 years ago

6.7.1-80c1951.0

2 years ago