Slide 1

Slide 1 text

How to build a messenger for Android? Rakhimov Andrii Lead Android Engineer at Lalafo [email protected]

Slide 2

Slide 2 text

➔ Story ➔ Transport ➔ Properties of transport and protocol ➔ Architecture ➔ MVP & MVI ➔ Learnings Agenda

Slide 3

Slide 3 text

Story Story

Slide 4

Slide 4 text

Story

Slide 5

Slide 5 text

Features ➔ Real time send/delivery 1:1, 1:n

Slide 6

Slide 6 text

Features ➔ Real time send/delivery 1:1, 1:n ➔ Image/File sending

Slide 7

Slide 7 text

Features ➔ Real time send/delivery 1:1, 1:n ➔ Image/File sending ➔ Delivery status

Slide 8

Slide 8 text

Features ➔ Real time send/delivery 1:1, 1:n ➔ Image/File sending ➔ Delivery status ➔ Typing

Slide 9

Slide 9 text

Features ➔ Real time send/delivery 1:1, 1:n ➔ Image/File sending ➔ Delivery status ➔ Typing ➔ Online status

Slide 10

Slide 10 text

Features ➔ Real time send/delivery 1:1, 1:n ➔ Image/File sending ➔ Delivery status ➔ Typing ➔ Online status ➔ Pagination

Slide 11

Slide 11 text

Features ➔ Real time send/delivery 1:1, 1:n ➔ Image/File sending ➔ Delivery status ➔ Typing ➔ Online status ➔ Pagination ➔ Offline send

Slide 12

Slide 12 text

➔ Real time send/delivery 1:1, 1:n ➔ Image/File sending ➔ Delivery status ➔ Typing ➔ Online status ➔ Pagination ➔ Offline send ➔ Delete chat Features

Slide 13

Slide 13 text

➔ Real time send/delivery 1:1, 1:n ➔ Image/File sending ➔ Delivery status ➔ Typing ➔ Online status ➔ Pagination ➔ Offline send ➔ Delete chat ➔ Blacklist user Features

Slide 14

Slide 14 text

➔ Real time send/delivery 1:1, 1:n ➔ Image/File sending ➔ Delivery status ➔ Typing ➔ Online status ➔ Pagination ➔ Offline send ➔ Delete chat ➔ Blacklist user ➔ Different types of chats ➔ Search Features

Slide 15

Slide 15 text

➔ Real time send/delivery 1:1, 1:n ➔ Image/File sending ➔ Delivery status ➔ Typing ➔ Online status ➔ Pagination ➔ Offline send ➔ Delete chat ➔ Blacklist user ➔ Different types of chats ➔ Search ➔ Chat with Lalafo Features

Slide 16

Slide 16 text

➔ Real time send/delivery 1:1, 1:n ➔ Image/File sending ➔ Delivery status ➔ Typing ➔ Online status ➔ Pagination ➔ Offline send ➔ Delete chat ➔ Blacklist user ➔ Different types of chats ➔ Search ➔ Chat with Lalafo ➔ Automate response ➔ ... Features

Slide 17

Slide 17 text

Telegram

Slide 18

Slide 18 text

Telegram Codebase 546 743 LOC

Slide 19

Slide 19 text

Variety of market solutions

Slide 20

Slide 20 text

Transport

Slide 21

Slide 21 text

HTTP

Slide 22

Slide 22 text

HTTP

Slide 23

Slide 23 text

HTTP + Simple and widely used

Slide 24

Slide 24 text

HTTP + Simple and widely used - Reopening connection is expensive

Slide 25

Slide 25 text

HTTP + Simple and widely used - Reopening connection is expensive - Unnecessary requests to server

Slide 26

Slide 26 text

HTTP + Simple and widely used - Reopening connection is expensive - Unnecessary requests to server - Slow, not real-time

Slide 27

Slide 27 text

HTTP + Firebase Cloud Messaging

Slide 28

Slide 28 text

HTTP + Firebase Cloud Messaging + Close to real-time

Slide 29

Slide 29 text

HTTP + Firebase Cloud Messaging + Close to real-time + Emulation of bidirectional communication

Slide 30

Slide 30 text

HTTP + Firebase Cloud Messaging + Close to real-time + Emulation of bidirectional communication - Third party service

Slide 31

Slide 31 text

HTTP + Firebase Cloud Messaging + Close to real-time + Emulation of bidirectional communication - Third party service - HTTP downsides

Slide 32

Slide 32 text

Bidirectional transport

Slide 33

Slide 33 text

Bidirectional transport

Slide 34

Slide 34 text

Messaging protocols

Slide 35

Slide 35 text

Socket.IO (Websocket under the hood) + Fast

Slide 36

Slide 36 text

Socket.IO (Websocket under the hood) + Fast + Bidirectional

Slide 37

Slide 37 text

Socket.IO (Websocket under the hood) + Fast + Bidirectional + Support web

Slide 38

Slide 38 text

Socket.IO (Websocket under the hood) + Fast + Bidirectional + Support web + Do not waste connection and resources

Slide 39

Slide 39 text

Socket.IO (Websocket under the hood) + Fast + Bidirectional + Reconnects + Support web + Do not waste connection and resources - 200+ issues for Android lib, not maintained for 2 years

Slide 40

Slide 40 text

Best Practices To work With Bidirectional Transport

Slide 41

Slide 41 text

Acknowledgment

Slide 42

Slide 42 text

Acknowledgment

Slide 43

Slide 43 text

Duplicate messages

Slide 44

Slide 44 text

Duplicate messages

Slide 45

Slide 45 text

Time sync and message order

Slide 46

Slide 46 text

Time sync and message order ➔ Validate if time is differed widely, and send appropriate time from backend ➔ Store diff on client ➔ Send and show messages accordingly

Slide 47

Slide 47 text

Battery concerns

Slide 48

Slide 48 text

Battery concerns ProcessLifecycleOwner.get() .getLifecycle() .addObserver(new LifecycleObserver() { @OnLifecycleEvent(Lifecycle.Event.ON_START) public void onEnterForeground() { messengerClient.connect(); } @OnLifecycleEvent(Lifecycle.Event.ON_STOP) public void onEnterBackground() { messengerClient.disconnect(); } });

Slide 49

Slide 49 text

Battery concerns ➔ Don’t keep socket connection open, after user leaves the app

Slide 50

Slide 50 text

Battery concerns ➔ Don’t keep socket connection open, after user leaves the app ➔ Don’t use foreground Service to keep connection open

Slide 51

Slide 51 text

Battery concerns ➔ Don’t keep socket connection open, after user leaves the app ➔ Don’t use foreground Service to keep connection open ➔ Use Firebase Cloud Messaging to notify the app about new messages

Slide 52

Slide 52 text

Battery concerns ➔ Don’t keep socket connection open, after user leaves the app ➔ Don’t use foreground Service to keep connection open ➔ Use Firebase Cloud Messaging to notify the app about new messages ➔ Use WorkManager to prefetch the messages after FCM notification

Slide 53

Slide 53 text

Startup Time public class App extends MultiDexApplication { … @Override public void onCreate() { super.onCreate(); initWhenIdle(); } private void initWhenIdle() { Looper.myQueue().addIdleHandler(() -> { appComponent.socketMessageHandler().start(); return false; }); } }

Slide 54

Slide 54 text

How to choose and design a transport? ➔ Determine your business needs and choose appropriate transport

Slide 55

Slide 55 text

How to choose and design a transport? ➔ Determine your business needs and choose appropriate transport ◆ Time

Slide 56

Slide 56 text

How to choose and design a transport? ➔ Determine your business needs and choose appropriate transport ◆ Time ◆ Speed

Slide 57

Slide 57 text

How to choose and design a transport? ➔ Determine your business needs and choose appropriate transport ◆ Time ◆ Speed ◆ Security

Slide 58

Slide 58 text

How to choose and design a transport? ➔ Determine your business needs and choose appropriate transport ◆ Time ◆ Speed ◆ Security ◆ Features

Slide 59

Slide 59 text

How to choose and design a transport? ➔ Determine your business needs and choose appropriate transport ◆ Time ◆ Speed ◆ Security ◆ Features ◆ Web support

Slide 60

Slide 60 text

How to choose and design a transport? ➔ Determine your business needs and choose appropriate transport ◆ Time ◆ Speed ◆ Security ◆ Features ◆ Web support ➔ Remember about Android(battery, startup time)

Slide 61

Slide 61 text

Architecture

Slide 62

Slide 62 text

Architecture ➔ Extensible and composable

Slide 63

Slide 63 text

Architecture ➔ Extensible and composable ➔ Maintainable

Slide 64

Slide 64 text

Architecture ➔ Extensible and composable ➔ Maintainable ➔ Reusable

Slide 65

Slide 65 text

Architecture ➔ Extensible and composable ➔ Maintainable ➔ Reusable ➔ Testable

Slide 66

Slide 66 text

Clean Architecture Dependency rule

Slide 67

Slide 67 text

Architecture

Slide 68

Slide 68 text

MVP & MVI

Slide 69

Slide 69 text

MVP & MVI

Slide 70

Slide 70 text

MVP & MVI

Slide 71

Slide 71 text

MVP & MVI

Slide 72

Slide 72 text

MVP issues ➔ Many events can trigger same UI changes as result broken UI

Slide 73

Slide 73 text

MVP issues ➔ Many events can trigger same UI changes as result broken UI ➔ Scales badly on huge and complex screens, presenters polluted with logic

Slide 74

Slide 74 text

MVP issues ➔ Many events can trigger same UI changes as result broken UI ➔ Scales badly on huge and complex screens, presenters polluted with logic ➔ Testability

Slide 75

Slide 75 text

MVP & MVI

Slide 76

Slide 76 text

MVVM & MVI

Slide 77

Slide 77 text

MVP & MVI

Slide 78

Slide 78 text

/** * Input Actions */ sealed class Action { object LoadNextPageAction : Action() data class ErrorLoadingPageAction(val error: Throwable, val page: Int) : Action() data class PageLoadedAction(val itemsLoaded: List, val page: Int) : Action() data class StartLoadingNextPage(val page: Int) : Action() } StateMachine Actions

Slide 79

Slide 79 text

StateMachine States sealed class State { object LoadingFirstPageState : State() data class ErrorLoadingFirstPageState(val errorMessage: String) : State() data class ShowContentState(val items: List, val page: Int) : State() }

Slide 80

Slide 80 text

StateMachine Setup class PaginationStateMachine @Inject constructor(private val githubOperations: GithubOperatinos) { val input: Relay = PublishRelay.create() val state: Observable = input .reduxStore( initialState = State.LoadingFirstPageState, sideEffects = listOf( ::loadFirstPageSideEffect, ::loadNextPageSideEffect, ::showAndHideLoadingErrorSideEffect ), reducer = ::reducer )

Slide 81

Slide 81 text

StateMachine Side Effect /** * Load the first Page */ private fun loadFirstPageSideEffect(actions: Observable, state: StateAccessor): Observable { return actions.ofType(Action.LoadFirstPageAction::class.java) .filter { state() !is ContainsItems } // If first page has already been loaded, do nothing .switchMap { val state = state() val nextPage = (if (state is ContainsItems) state.page else 0) + 1 githubOperations.loadRepositories(nextPage) .map { result -> PageLoadedAction(itemsLoaded = result.items, page = nextPage) } .onErrorReturn { error -> ErrorLoadingPageAction(error, nextPage) } .startWith(StartLoadingNextPage(nextPage)) } }

Slide 82

Slide 82 text

StateMachine Reducer /** * The state reducer. * Takes Actions and the current state to calculate the new state. */ private fun reducer(state: State, action: Action): State { return when (action) { is StartLoadingNextPage -> State.LoadingFirstPageState is PageLoadedAction -> State.ShowContentState(items = action.items, page = action.page) is ErrorLoadingPageAction -> State.ErrorLoadingFirstPageState(action.error.localizedMessage) } }

Slide 83

Slide 83 text

Displaying state open fun render(state: PaginationStateMachine.State) = when (state) { PaginationStateMachine.State.LoadingFirstPageState -> { recyclerView.gone() loading.visible() error.gone() } is PaginationStateMachine.State.ShowContentState -> { showRecyclerView(items = state.items, showLoadingNext = false) } is PaginationStateMachine.State.ErrorLoadingFirstPageState -> { recyclerView.gone() loading.gone() error.visible() snackBar?.dismiss() } } Compose Dreams

Slide 84

Slide 84 text

Performance matters

Slide 85

Slide 85 text

DistinctUntilChanged val state: Observable = input .reduxStore( initialState = State.LoadingFirstPageState, sideEffects = listOf( ::loadFirstPageSideEffect, ::loadNextPageSideEffect, ::showAndHideLoadingErrorSideEffect ), reducer = ::reducer ) .distinctUntilChanged()

Slide 86

Slide 86 text

Learnings ➔ Fair estimates

Slide 87

Slide 87 text

Fair estimates

Slide 88

Slide 88 text

Learnings ➔ Fair estimates ➔ Use appropriate technology for the task

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

Learnings ➔ Fair estimates ➔ Use appropriate technology for the task ➔ Design for flexibility

Slide 91

Slide 91 text

Learnings ➔ Fair estimates ➔ Use appropriate technology for the task ➔ Design for flexibility ➔ Start small and validate hypothesis early

Slide 92

Slide 92 text

Cost of change

Slide 93

Slide 93 text

Learnings ➔ Fair estimates ➔ Use appropriate technology for the task ➔ Design for flexibility ➔ Start small and validate hypothesis early

Slide 94

Slide 94 text

Thank you! Rakhimov Andrii Lead Android Engineer at Lalafo [email protected] https://github.com/ar-g