Convert Figma logo to code with AI

ReactiveX logoRxKotlin

RxJava bindings for Kotlin

7,028
454
7,028
32

Top Related Projects

47,834

RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.

24,320

Reactive Programming in Swift

Library support for Kotlin coroutines

RxJava binding APIs for Android's UI widgets.

Mavericks: Android on Autopilot

Quick Overview

RxKotlin is a Kotlin implementation of ReactiveX, a library for composing asynchronous and event-based programs using observable sequences. It extends the ReactiveX family to Kotlin, providing a powerful set of tools for handling asynchronous data streams and event-based programming in a functional, reactive style.

Pros

  • Seamless integration with Kotlin's language features and syntax
  • Powerful operators for transforming, combining, and manipulating observable streams
  • Excellent for managing complex asynchronous operations and event-driven programming
  • Strong interoperability with Java and Android development

Cons

  • Steep learning curve for developers new to reactive programming
  • Can lead to overly complex code if not used judiciously
  • Potential performance overhead for simple use cases
  • Limited documentation compared to RxJava

Code Examples

  1. Creating and subscribing to an Observable:
import io.reactivex.rxkotlin.toObservable

val numbers = listOf(1, 2, 3, 4, 5)
numbers.toObservable()
    .subscribe { println(it) }
  1. Using operators to transform data:
import io.reactivex.rxkotlin.toObservable

val numbers = listOf(1, 2, 3, 4, 5)
numbers.toObservable()
    .map { it * 2 }
    .filter { it > 5 }
    .subscribe { println(it) }
  1. Combining multiple Observables:
import io.reactivex.Observable

val obs1 = Observable.just(1, 2, 3)
val obs2 = Observable.just(4, 5, 6)

Observable.zip(obs1, obs2) { a, b -> a + b }
    .subscribe { println(it) }

Getting Started

To start using RxKotlin in your project, add the following dependency to your build.gradle file:

dependencies {
    implementation 'io.reactivex.rxjava2:rxkotlin:2.4.0'
}

For Maven users, add this to your pom.xml:

<dependency>
    <groupId>io.reactivex.rxjava2</groupId>
    <artifactId>rxkotlin</artifactId>
    <version>2.4.0</version>
</dependency>

After adding the dependency, you can start using RxKotlin in your Kotlin code by importing the necessary classes and functions from the io.reactivex package.

Competitor Comparisons

47,834

RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.

Pros of RxJava

  • More mature and widely adopted in the Java ecosystem
  • Extensive documentation and community support
  • Broader range of operators and utilities

Cons of RxJava

  • Verbose syntax compared to Kotlin-specific implementations
  • Lacks some Kotlin-specific language features and idioms
  • May require additional setup for Kotlin projects

Code Comparison

RxJava:

Observable.just("Hello", "World")
    .map(String::toUpperCase)
    .subscribe(System.out::println);

RxKotlin:

Observable.just("Hello", "World")
    .map { it.toUpperCase() }
    .subscribe { println(it) }

RxKotlin provides a more concise syntax leveraging Kotlin's language features, such as lambda expressions and type inference. While both libraries offer similar functionality, RxKotlin is tailored specifically for Kotlin projects, providing a more idiomatic and streamlined experience for Kotlin developers.

RxJava remains a solid choice for Java projects and mixed Java/Kotlin codebases, offering a wealth of resources and a large community. However, for pure Kotlin projects, RxKotlin may provide a more natural fit, integrating seamlessly with Kotlin's language features and conventions.

24,320

Reactive Programming in Swift

Pros of RxSwift

  • More mature and stable ecosystem for iOS development
  • Extensive documentation and community support
  • Better integration with Swift's type system and language features

Cons of RxSwift

  • Steeper learning curve for developers new to reactive programming
  • Potential performance overhead in complex scenarios
  • Limited compatibility with older iOS versions

Code Comparison

RxSwift:

Observable.from([1, 2, 3, 4, 5])
    .filter { $0 % 2 == 0 }
    .map { $0 * 2 }
    .subscribe(onNext: { print($0) })

RxKotlin:

Observable.fromIterable(listOf(1, 2, 3, 4, 5))
    .filter { it % 2 == 0 }
    .map { it * 2 }
    .subscribe { println(it) }

Both RxSwift and RxKotlin provide similar functionality for reactive programming in their respective languages. The syntax is quite similar, with minor differences in method names and language-specific features. RxSwift benefits from Swift's strong type system and iOS-specific optimizations, while RxKotlin leverages Kotlin's concise syntax and multiplatform capabilities. Developers familiar with one can easily transition to the other, as the core concepts remain consistent across ReactiveX implementations.

Library support for Kotlin coroutines

Pros of kotlinx.coroutines

  • Lightweight and more idiomatic Kotlin syntax
  • Built-in support for structured concurrency
  • Better integration with Kotlin's suspend functions

Cons of kotlinx.coroutines

  • Steeper learning curve for developers new to coroutines
  • Less extensive ecosystem compared to RxKotlin
  • Limited support for backpressure handling

Code Comparison

kotlinx.coroutines:

suspend fun fetchData() = coroutineScope {
    val result1 = async { api.fetchItem1() }
    val result2 = async { api.fetchItem2() }
    result1.await() + result2.await()
}

RxKotlin:

fun fetchData(): Single<Result> {
    return Single.zip(
        api.fetchItem1().subscribeOn(Schedulers.io()),
        api.fetchItem2().subscribeOn(Schedulers.io()),
        { r1, r2 -> r1 + r2 }
    )
}

Both libraries offer powerful concurrency tools for Kotlin developers. kotlinx.coroutines provides a more native Kotlin experience with structured concurrency, while RxKotlin offers a rich set of operators and better backpressure handling. The choice between them often depends on project requirements and team familiarity with reactive programming concepts.

RxJava binding APIs for Android's UI widgets.

Pros of RxBinding

  • Specifically designed for Android UI components, offering seamless integration with Android views
  • Provides pre-built bindings for common Android UI elements, reducing boilerplate code
  • Offers more granular control over UI-specific events and interactions

Cons of RxBinding

  • Limited to Android platform, whereas RxKotlin is more versatile and can be used in various Kotlin projects
  • Smaller community and fewer resources compared to the more general-purpose RxKotlin
  • May require additional learning curve for developers not familiar with Android-specific reactive programming

Code Comparison

RxBinding (Android-specific):

RxView.clicks(button)
    .subscribe { /* Handle button click */ }

RxKotlin (General-purpose):

Observable.fromCallable { /* Perform operation */ }
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe { /* Handle result */ }

RxBinding focuses on UI-specific reactive programming, while RxKotlin provides a more general-purpose reactive programming solution for Kotlin projects. RxBinding is ideal for Android developers looking to simplify UI interactions, while RxKotlin offers broader applicability across different types of Kotlin applications.

Mavericks: Android on Autopilot

Pros of Mavericks

  • Specifically designed for Android development, offering a more tailored solution
  • Provides a robust architecture pattern (MvRx) for building complex UIs
  • Integrates well with Jetpack Compose for modern Android UI development

Cons of Mavericks

  • Steeper learning curve for developers not familiar with the MvRx pattern
  • Less flexibility compared to RxKotlin's general-purpose reactive programming
  • Limited to Android development, whereas RxKotlin can be used in various Kotlin projects

Code Comparison

Mavericks (MvRx) example:

class MyViewModel(initialState: MyState) : MavericksViewModel<MyState>(initialState) {
    fun updateData() = setState { copy(data = "New Data") }
}

RxKotlin example:

val observable = Observable.just("Data")
    .map { it.uppercase() }
    .subscribe { println(it) }

Mavericks focuses on state management and UI updates, while RxKotlin provides general reactive programming constructs. Mavericks is more opinionated about architecture, whereas RxKotlin offers more flexibility but requires more setup for similar functionality in Android development.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

RxKotlin

Maven Central

Kotlin Extensions for RxJava

RxKotlin is a lightweight library that adds convenient extension functions to RxJava. You can use RxJava with Kotlin out-of-the-box, but Kotlin has language features (such as extension functions) that can streamline usage of RxJava even more. RxKotlin aims to conservatively collect these conveniences in one centralized library, and standardize conventions for using RxJava with Kotlin.

import io.reactivex.rxjava3.kotlin.subscribeBy
import io.reactivex.rxjava3.kotlin.toObservable

fun main() {

    val list = listOf("Alpha", "Beta", "Gamma", "Delta", "Epsilon")

    list.toObservable() // extension function for Iterables
            .filter { it.length >= 5 }
            .subscribeBy(  // named arguments for lambda Subscribers
                    onNext = { println(it) },
                    onError =  { it.printStackTrace() },
                    onComplete = { println("Done!") }
            )

}

Contributing

:warning: (16/10/2023) We are currently not accepting contributions due to lack of developers capable of handling them in a reasonable manner.

Since Kotlin makes it easy to implement extensions for anything and everything, this project has to be conservative in what features are in scope. Intentions to create syntactic sugar can quickly regress into syntactic saccharin, and such personal preferences belong in one's internal domain rather than an OSS library.

Here are some basic guidelines to determine whether your contribution might be in scope for RxKotlin:

  • Is this intended feature already in RxJava?
    • If no, propose the operator in RxJava.
    • If yes, can Kotlin streamline the operator further?
  • Does this substantially reduce the amount of boilerplate code?
  • Does this make an existing operator easier to use?
  • Does RxJava not contain this feature due to Java language limitations, or because of a deliberate decision to not include it?

Kotlin Slack Channel

Join us on the #rx channel in Kotlin Slack!

https://kotlinlang.slack.com/messages/rx

Support for RxJava 3.x, RxJava 2.x and RxJava 1.x

Use RxKotlin 3.x versions to target RxJava 3.x.

  • The 3.x version is active.

Use RxKotlin 2.x versions to target RxJava 2.x.

  • The 2.x version of RxJava and RxKotlin is in maintenance mode and will be supported only through bugfixes. No new features or behavior changes will be accepted or applied.

Use RxKotlin 1.x versions to target RxJava 1.x.

  • The 1.x version of RxJava and RxKotlin reached end-of-life. No further development, support, maintenance, PRs or updates will happen.

The maintainers do not update the RxJava dependency version for every minor or patch RxJava release, so you should explicitly add the desired RxJava dependency version to your pom.xml or build.gradle(.kts).

Binaries

Binaries and dependency information for Maven, Ivy, Gradle and others can be found at http://search.maven.org.

RxKotlin 3.x Build Status

Example for Maven:

<dependency>
    <groupId>io.reactivex.rxjava3</groupId>
    <artifactId>rxkotlin</artifactId>
    <version>3.x.y</version>
</dependency>

Example for Gradle:

implementation("io.reactivex.rxjava3:rxkotlin:3.x.y")

RxKotlin 2.x Build Status

Example for Maven:

<dependency>
    <groupId>io.reactivex.rxjava2</groupId>
    <artifactId>rxkotlin</artifactId>
    <version>2.x.y</version>
</dependency>

Example for Gradle:

implementation("io.reactivex.rxjava2:rxkotlin:2.x.y")

RxKotlin 1.x

Example for Maven:

<dependency>
    <groupId>io.reactivex</groupId>
    <artifactId>rxkotlin</artifactId>
    <version>1.x.y</version>
</dependency>

Example for Gradle:

implementation("io.reactivex:rxkotlin:1.x.y")

Building with JitPack

You can also use Gradle or Maven with JitPack to build directly off a snapshot, branch, or commit of this repository.

For example, to build off the 3.x branch, use this setup for Gradle:

repositories {
    maven { url 'https://jitpack.io' }
}

dependencies {
    implementation 'com.github.ReactiveX:RxKotlin:3.x-SNAPSHOT'
}

Use this setup for Maven:

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

    <dependency>
	    <groupId>com.github.ReactiveX</groupId>
	    <artifactId>RxKotlin</artifactId>
	    <version>3.x-SNAPSHOT</version>
	</dependency>

Learn more about building this project with JitPack here.

Extensions

Target TypeMethodReturn TypeDescription
BooleanArraytoObservable()ObservableTurns a Boolean array into an Observable
ByteArraytoObservable()ObservableTurns a Byte array into an Observable
ShortArraytoObservable()ObservableTurns a Short array into an Observable
IntArraytoObservable()ObservableTurns an Int array into an Observable
LongArraytoObservable()ObservableTurns a Long array into an Observable
FloatArraytoObservable()ObservableTurns a Float array into an Observable
DoubleArraytoObservable()ObservableTurns a Double array into an Observable
ArraytoObservable()ObservableTurns a T array into an Observable
IntProgressiontoObservable()ObservableTurns an IntProgression into an Observable
IterabletoObservable()ObservableTurns an Iterable<T> into an Observable
IteratortoObservable()ObservableTurns an Iterator<T> into an Observable
ObservableflatMapSequence()ObservableFlat maps each T emission to a Sequence<R>
Observable<Pair<A,B>>toMap()Single<Map<A,B>>Collects Pair<A,B> emissions into a Map<A,B>
Observable<Pair<A,B>>toMultimap()Single<Map<A, List<B>>Collects Pair<A,B> emissions into a Map<A,List<B>>
Observable<Observable>mergeAll()ObservableMerges all Observables emitted from an Observable
Observable<Observable>concatAll()ObservableConcatenates all Observables emitted from an Observable
Observable<Observable>switchLatest()ObservableEmits from the last emitted Observable
Observable<*>cast()ObservableCasts all emissions to the reified type
Observable<*>ofType()ObservableFilters all emissions to only the reified type
Iterable<Observable>merge()ObservableMerges an Iterable of Observables into a single Observable
Iterable<Observable>mergeDelayError()ObservableMerges an Iterable of Observables into a single Observable, but delays any error
BooleanArraytoFlowable()FlowableTurns a Boolean array into an Flowable
ByteArraytoFlowable()FlowableTurns a Byte array into an Flowable
ShortArraytoFlowable()FlowableTurns a Short array into an Flowable
IntArraytoFlowable()FlowableTurns an Int array into an Flowable
LongArraytoFlowable()FlowableTurns a Long array into an Flowable
FloatArraytoFlowable()FlowableTurns a Float array into an Flowable
DoubleArraytoFlowable()FlowableTurns a Double array into an Flowable
ArraytoFlowable()FlowableTurns a T array into an Flowable
IntProgressiontoFlowable()FlowableTurns an IntProgression into an Flowable
IterabletoFlowable()FlowableTurns an Iterable<T> into an Flowable
IteratortoFlowable()FlowableTurns an Iterator<T> into an Flowable
FlowableflatMapSequence()FlowableFlat maps each T emission to a Sequence<R>
Flowable<Pair<A,B>>toMap()Single<Map<A,B>>Collects Pair<A,B> emissions into a Map<A,B>
Flowable<Pair<A,B>>toMultimap()Single<Map<A, List<B>>>Collects Pair<A,B> emissions into a Map<A,List<B>>
Flowable<Flowable>mergeAll()FlowableMerges all Flowables emitted from an Flowable
Flowable<Flowable>concatAll()FlowableConcatenates all Flowables emitted from an Flowable
Flowable<Flowable>switchLatest()FlowableEmits from the last emitted Flowable
Flowablecast()FlowableCasts all emissions to the reified type
FlowableofType()FlowableFilters all emissions to only the reified type
Iterable<Flowable>merge()FlowableMerges an Iterable of Flowables into a single Flowable
Iterable<Flowable>mergeDelayError()FlowableMerges an Iterable of Flowables into a single Flowable, but delays any error
Singlecast()SingleCasts all emissions to the reified type
Observable<Single>mergeAllSingles()ObservableMerges all Singles emitted from an Observable
Flowable<Single>mergeAllSingles()FlowableMerges all Singles emitted from a Flowable
Maybecast()MaybeCasts any emissions to the reified type
MaybeofType()MaybeFilters any emission that is the reified type
Observable<Maybe>mergeAllMaybes()ObservableMerges all emitted Maybes
Flowable<Maybe>mergeAllMaybes()FlowableMerges all emitted Maybes
ActiontoCompletable()CompletableTurns an Action into a Completable
CallabletoCompletable()CompletableTurns a Callable into a Completable
FuturetoCompletable()CompletableTurns a Future into a Completable
(() -> Any)toCompletable()CompletableTurns a (() -> Any) into a Completable
ObservablemergeAllCompletables()Completable>Merges all emitted Completables
FlowablemergeAllCompletables()CompletableMerges all emitted Completables
ObservablesubscribeBy()DisposableAllows named arguments to construct an Observer
FlowablesubscribeBy()DisposableAllows named arguments to construct a Subscriber
SinglesubscribeBy()DisposableAllows named arguments to construct a SingleObserver
MaybesubscribeBy()DisposableAllows named arguments to construct a MaybeObserver
CompletablesubscribeBy()DisposableAllows named arguments to construct a CompletableObserver
ObservableblockingSubscribeBy()UnitAllows named arguments to construct a blocking Observer
FlowableblockingSubscribeBy()UnitAllows named arguments to construct a blocking Subscriber
SingleblockingSubscribeBy()UnitAllows named arguments to construct a blocking SingleObserver
MaybeblockingSubscribeBy()UnitAllows named arguments to construct a blocking MaybeObserver
CompletableblockingSubscribeBy()UnitAllows named arguments to construct a blocking CompletableObserver
DisposableaddTo()DisposableAdds a Disposable to the specified CompositeDisposable
CompositeDisposableplusAssign()DisposableOperator function to add a Disposable to thisCompositeDisposable

SAM Helpers (made obsolete since Kotlin 1.4)

These methods have been made obsolete with new type inference algorithm in Kotlin 1.4. They will be removed in some future RxKotlin version.

To help cope with the SAM ambiguity issue when using RxJava with Kotlin, there are a number of helper factories and extension functions to workaround the affected operators.

Observables.zip()
Observables.combineLatest()
Observable#zipWith()
Observable#withLatestFrom()
Flowables.zip()
Flowables.combineLatest()
Flowable#zipWith()
Flowable#withLatestFrom()
Singles.zip()
Single#zipWith()
Maybes.zip()

Usage with Other Rx Libraries

RxKotlin can be used in conjunction with other Rx and Kotlin libraries, such as RxAndroid, RxBinding, and TornadoFX/RxKotlinFX. These libraries and RxKotlin are modular, and RxKotlin is merely a set of extension functions to RxJava that can be used with these other libraries. There should be no overlap or dependency issues.

Other Resources

Learning RxJava Packt Book

Chapter 12 of Learning RxJava covers RxKotlin and Kotlin idioms with RxJava.

Reactive Programming in Kotlin Packt Book

The book Reactive Programming in Kotlin mainly focuses on RxKotlin, as well as learning reactive programming with Kotlin.