Git Product home page Git Product logo

aws-samples / amazon-ivs-chat-for-android-demo Goto Github PK

View Code? Open in Web Editor NEW
12.0 13.0 2.0 308 KB

A demo Android app built with Kotlin that shows how to implement a basic video + chat application using Amazon IVS and the AWS serverless application model (SAM).

License: MIT No Attribution

Kotlin 100.00%
amazon-ivs android aws chat kotlin live-chat live-streams live-video video websockets

amazon-ivs-chat-for-android-demo's Introduction

Amazon IVS Chat for Android Demo

A demo Android phone application intended for use as an educational tool to demonstrate how a simple live video and chat application can be built with Amazon IVS video and chat.

Amazon IVS Chat for Android Demo

This project is intended for education purposes only and not for production usage.

The demo showcases how you can implement a simple live streaming application with video and chat using Amazon IVS. Viewers are asked to enter their name the first time they begin chatting. Chat users can send plain text messages, emojis, and stickers. Chat moderators can delete messages and kick users.

Prerequisites

Setup

To open the demo Android app, follow these instructions:

  1. Clone this repository to your local machine
  2. Open app/build.gradle file and set constants for:
    • Amazon IVS Playback URL STREAM_URL
      • A sample playback url for a 24/7 livestream is pre-filled. You may add a playback url to use a custom livestream in this demo.
    • Amazon IVS Chat Demo backend endpoint API_URL
    • Chat room id CHAT_ROOM_ID
      • The ID (or ARN) of the Amazon IVS Chat Room that the app should use.
      • You must create an Amazon IVS Chat Room to get a chat room ID/ARN. Refer to Getting Started with Amazon IVS Chat for a detailed guide.
  3. Open the constants file at app/java/com/amazon/ivs/chatdemo/common/Constants.kt and set constants for:
    • The AWS region of your chat room REGION_URL
      • This is set to us-west-2 by default.
  4. Build and install the .apk on your device / emulator

Known issues and limitations

  • The application is meant for demonstration purposes and not for production use.
  • This application is only tested in the us-west-2 (Oregon) region. Additional regions may be supported depending on service availability.

About Amazon IVS

Amazon Interactive Video Service (Amazon IVS) is a managed live streaming and stream chat solution that is quick and easy to set up, and ideal for creating interactive video experiences. Learn more.

Security

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

amazon-ivs-chat-for-android-demo's People

Contributors

amazon-auto avatar egorbunovs avatar hectormatos2011 avatar slee-aws avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

amazon-ivs-chat-for-android-demo's Issues

Execute connect method in onResume won't bring the connection back after disconnect execution in onPause

I'm building an app with chat functionalities using Amazon IVS Chat Client Messaging SDK and I stucked with handling lifecycle of the chat room and web socket connection.

According to the documentation Disconnect from a Chat Room:

Because the WebSocket connection will stop working after a short time when the application is in a background state, we recommend that you manually connect/disconnect when transitioning from/to a background state. To do so, match the room.connect() call in the onResume() lifecycle method, on Android Activity or Fragment, with a room.disconnect() call in the onPause() lifecycle method.

So I implemented that way, but I never get connected state back again (I need to close activity and open it again, then all works fine):

๐ŸŸข Room 58a2b001-7c9c-4c2b-935b-ef07e74cc2c3 (CONNECTING) Changed state: DISCONNECTED -> CONNECTING
๐ŸŸค Room 58a2b001-7c9c-4c2b-935b-ef07e74cc2c3 (CONNECTING) Calling onConnecting
๐ŸŸค Room 58a2b001-7c9c-4c2b-935b-ef07e74cc2c3 (CONNECTING) Requesting token
๐ŸŸค Room 58a2b001-7c9c-4c2b-935b-ef07e74cc2c3 (CONNECTING) Did receive token: ChatToken(token=CHAT_TOKEN_HERE#1, sessionExpirationTime=Fri Sep 08 13:20:24 GMT+02:00 2023, tokenExpirationTime=Fri Sep 08 13:20:24 GMT+02:00 2023)
๐ŸŸค Room 58a2b001-7c9c-4c2b-935b-ef07e74cc2c3 (CONNECTING) Connecting to web socket wss://edge.ivschat.us-east-1.amazonaws.com
๐ŸŸข Room 58a2b001-7c9c-4c2b-935b-ef07e74cc2c3 (CONNECTED) Changed state: CONNECTING -> CONNECTED
๐ŸŸค Room 58a2b001-7c9c-4c2b-935b-ef07e74cc2c3 (CONNECTED) Calling onConnected
๐ŸŸข Room 58a2b001-7c9c-4c2b-935b-ef07e74cc2c3 (CONNECTED) Connected
// App moves to ON_PAUSE state
๐ŸŸข Room 58a2b001-7c9c-4c2b-935b-ef07e74cc2c3 (DISCONNECTED) Changed state: CONNECTED -> DISCONNECTED
๐ŸŸค Room 58a2b001-7c9c-4c2b-935b-ef07e74cc2c3 (DISCONNECTED) Calling onDisconnected
๐ŸŸข Room 58a2b001-7c9c-4c2b-935b-ef07e74cc2c3 (DISCONNECTED) Did receive socket onClosing event: disconnect reason CLIENT_DISCONNECT, socket code 1000, socket reason: 
๐ŸŸข Room 58a2b001-7c9c-4c2b-935b-ef07e74cc2c3 (DISCONNECTED) Reconnect not needed: disconnect reason CLIENT_DISCONNECT
// APP moves to ON_RESUME state
๐ŸŸข Room 58a2b001-7c9c-4c2b-935b-ef07e74cc2c3 (CONNECTING) Changed state: DISCONNECTED -> CONNECTING
๐ŸŸค Room 58a2b001-7c9c-4c2b-935b-ef07e74cc2c3 (CONNECTING) Calling onConnecting
๐ŸŸค Room 58a2b001-7c9c-4c2b-935b-ef07e74cc2c3 (CONNECTING) Requesting token

I keep seeing Requesting token. For the demo app I found here, I noticed that you didn't execute disconnect method explicitly when app lifecycle changes state to Pause/Stop. Was it implemented on purpose?

Please take a look on implementation of wrapper for ChatRoom class. Did I miss something?.:

internal class ChatRemoteApiImpl @Inject constructor(
    private val webinarRemoteApi: WebinarRemoteApi,
    private val chatClientStateMapper: ChatClientStateMapper,
    private val chatMessageRemoteMapper: ChatMessageRemoteMapper,
) : ChatRemoteApi {

    private var _chatRoom: ChatRoom? = null
        set(value) {
            field = value
            if (value != null) {
                mutableChatRoomFlow.tryEmit(value)
            }
        }

    private val isConnected: Boolean
        get() {
            return chatRoom.state == ChatRoom.State.CONNECTED
        }

    private val mutableChatRoomFlow = bufferedFlow<ChatRoom>(replay = 1)

    private val chatRoom: ChatRoom
        get() {
            return checkNotNull(_chatRoom) { "Chat Room need to be initialized" }
        }

    override val stateChange: Flow<State>
        get() = mutableChatRoomFlow.take(1)
            .flatMapLatest { chatRoom ->
                chatRoom.stateChanges()
                    .map(chatClientStateMapper::map)
            }

    override val messages: Flow<ChatMessageResponse>
        get() = mutableChatRoomFlow.take(1)
            .flatMapLatest { chatRoom ->
                chatRoom.receivedMessages()
                    .map(chatMessageRemoteMapper::map)
            }

    override suspend fun createClient(params: ChatClientParams) {
        val chatTokenProvider: suspend () -> ChatToken = suspend {
            val response = webinarRemoteApi.createChatToken(params.webinarId, params.userId)
            with(response) {
                ChatToken(
                    token,
                    tokenExpirationTime.toDate(),
                    sessionExpirationTime.toDate(),
                )
            }
        }

        _chatRoom = ChatRoom(
            regionOrUrl = params.region,
            tokenProvider = chatTokenProvider,
        )
    }

    override fun connect() {       // call onResume
        if (!isConnected) {
            chatRoom.connect()
        }
    }

    override fun disconnect() {   // call onPause
        chatRoom.disconnect()
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.