# Observables versus Reactive

**URL:** <https://discourse.julialang.org/t/observables-versus-reactive/5854>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 12, 2017, 3:47pm UTC](https://discourse.julialang.org/t/observables-versus-reactive/5854 "2017-09-12T15:47:30Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [September 12, 2017, 3:47pm UTC](https://discourse.julialang.org/t/observables-versus-reactive/5854/1 "2017-09-12T15:47:31Z")

</div>

Checkout [Observables.jl](https://github.com/JuliaGizmos/Observables.jl), looks cool and super useful.

I don’t think I really understand the difference between and Observable and a Reactive.Signal. Anyone here can explain? When would one be better than the other? Why? How? What can one do that the other doesn’t? Why is that special?

I like new and shiny things.

---

<div class="post-metadata">

**Author:** ![JackDevine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jackdevine/32/1048_2.png) [@JackDevine](https://discourse.julialang.org/u/JackDevine)\
**Post date:** [September 13, 2017, 5:54am UTC](https://discourse.julialang.org/t/observables-versus-reactive/5854/2 "2017-09-13T05:54:34Z")

</div>

Looks very cool, I quite like the packages that Shashi has worked on previously.

Quoting from the documentation:

How is it different from Reactive.jl?

The main difference is Signals are manipulated mostly by converting one signal to another. For example, with signals, you can construct a changing UI by creating a Signal of UI objects and rendering them as the signal changes. On the other hand, you can use an Observable both as an input and an output. You can arbitrarily attach outputs to inputs allowing structuring code in a signals-and-slots kind of pattern.

Another difference is Observables are synchronous, Signals are asynchronous. Observables may be better suited for an imperative style of programming.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [September 13, 2017, 7:27am UTC](https://discourse.julialang.org/t/observables-versus-reactive/5854/3 "2017-09-13T07:27:43Z")

</div>

Yea, I read that, but I _still_ don’t get it… Like, what do they mean with “Observable both as an input and an output”, an input to what?

---

<div class="post-metadata">

**Author:** ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)\
**Post date:** [September 13, 2017, 6:39pm UTC](https://discourse.julialang.org/t/observables-versus-reactive/5854/4 "2017-09-13T18:39:03Z")

</div>

I interpret that as follows:

```julia
using Observables

const input = Observable(1)
const output = Observable(0)

on(input) do x
    output[] = 2*x
end

on(output) do x
    println("output set to $x")
end

input[] = 1
input[] = 2
input[] = 10

```

This prints:

```nohighlight
output set to 2
output set to 4
output set to 20

```

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [September 13, 2017, 6:55pm UTC](https://discourse.julialang.org/t/observables-versus-reactive/5854/5 "2017-09-13T18:55:56Z")

</div>

Fine, but I don’t understand how that’s much better/different than:

```julia
using Reactive

const input = Signal(1)
const output = map(input) do x
    2x
end

foreach(output) do x
    println("output set to $x")
end

push!(input, 1)
push!(input, 2)
push!(input, 10)

```

Which also prints:

```julia
output set to 2
output set to 4
output set to 20

```

Like, what’s the novel feature here?

---

<div class="post-metadata">

**Author:** ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)\
**Post date:** [September 13, 2017, 8:40pm UTC](https://discourse.julialang.org/t/observables-versus-reactive/5854/6 "2017-09-13T20:40:56Z")

</div>

I think the difference is quite subtle, in the case of Reactive to get the output you actually need to create a new Signal (this is hidden by the `foreach`), while in Observables any Observable can call any number of functions without creating a new Observable.

To expand a bit on your question, for me the main difference is simplicity: every Observable basically stores a list of functions to call upon change, and these are just called synchronously without intervention of the task system.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [September 13, 2017, 9:31pm UTC](https://discourse.julialang.org/t/observables-versus-reactive/5854/7 "2017-09-13T21:31:56Z")

</div>

Interesting. I’m writing a semi-large GUI right now. I’m making heavy use of Reactive and GtkReactive, and I therefore have tons of signals everywhere. I’m curious about how it would look and work with these observables.

Thanks for the feedback @barche, but I’m still unclear on the main advantages Observables have over Signals… Are we talking better as-in observables are going to replace signals in the future, or just different use-cases? And if so, what would differ between the two use-cases?

---

<div class="post-metadata">

**Author:** ![tshort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tshort/32/43_2.png) [@tshort](https://discourse.julialang.org/u/tshort)\
**Post date:** [September 13, 2017, 9:43pm UTC](https://discourse.julialang.org/t/observables-versus-reactive/5854/8 "2017-09-13T21:43:30Z")

</div>

There’s also ReactiveBasics which acts synchronously but has the API of Reactive.jl.

[https://github.com/tshort/ReactiveBasics.jl](https://github.com/tshort/ReactiveBasics.jl)

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [September 13, 2017, 9:55pm UTC](https://discourse.julialang.org/t/observables-versus-reactive/5854/9 "2017-09-13T21:55:30Z")

</div>

Wow! Cool stuff! Irritating that I missed it.

---

<div class="post-metadata">

**Author:** ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)\
**Post date:** [September 14, 2017, 11:16am UTC](https://discourse.julialang.org/t/observables-versus-reactive/5854/10 "2017-09-14T11:16:59Z")

</div>

> [@yakir12](#):
>
> I’m still unclear on the main advantages Observables have over Signals… Are we talking better as-in observables are going to replace signals in the future, or just different use-cases? And if so, what would differ between the two use-cases?

The use-cases are different, in my opinion. Reactive is asynchronous, which might require a `yield` to force an update, as the following example illustrates:

```julia
using Reactive

the_value = 0

s = Signal(0)

s2 = map(s) do x
    global the_value
    # try with println, it interferes with the event queue and breaks this demo
    ccall(:jl_safe_printf,Void,(Ptr{UInt8}, Vararg{Int32}), "setting value to $x\n")
    the_value = x
end

push!(s, 1)

print("value after push is $the_value\n")
yield()
print("value after yield is $the_value\n")

```

So if you want `the_value` to change as soon as signal `s` is updated, the synchronous approach is a better fit, but if you want `the_value` to update in the background then an asynchronous system like Reactive is needed.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [September 14, 2017, 11:21am UTC](https://discourse.julialang.org/t/observables-versus-reactive/5854/11 "2017-09-14T11:21:24Z")

</div>

That sounds like that is the difference between `Reactive` and `ReactiveBasics.jl`. I’m sorry to insist, but I’m fishing for more than the asynchronous versus synchronous difference… Or maybe that’s just it?

---

<div class="post-metadata">

**Author:** ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)\
**Post date:** [September 14, 2017, 3:40pm UTC](https://discourse.julialang.org/t/observables-versus-reactive/5854/12 "2017-09-14T15:40:44Z")

</div>

As far as I can tell, the difference is conceptual: in the Reactive framework, signals are only connected to other signals, and input is triggered by pushing to the “source” signal. In your example, the `foreach` hides the fact that to do anything when `output` changes you need to create a new signal. So signals are aware of each other and connected to each other by mapping functions that are intended to transform the input signal into the output signal, thus forming a sort of processing pipeline.

Observables on the other hand are just values that can call functions when their value changes. There is no inherent network between observables, unless one of the observers (= just a Julia function) decides to update another observable.

Personally I would like to take the way Observables one step further: a changing value is just one possible event, so separating the event notification system from objects holding a value should be possible. This would allow for events that don’t have a value, such as notifying that a button was clicked, or events that have more than one value associated with them. A very basic prototype of how this might work is here:

[https://github.com/JuliaGizmos/Observables.jl/pull/6](https://github.com/JuliaGizmos/Observables.jl/pull/6)

CC @tim.holy and @shashi in case they missed this thread and want to “react” 😉
