title: Best way to build a "task queue" with RxJava
body: <p>Currently I'm working on a lot of network-related features. At the moment, I'm dealing with a network channel that allows me to send 1 single piece of information at a time, and I have to wait for it to be acknowledged before I can send the next piece of information. I'm representing the server with <code>1..n</code> connected clients.</p>
  
  <p>Some of these messages, I have to send in chunks, which is fairly easy to do with RxJava. Currently my "writing" method looks sort of like this:</p>
  
  <pre><code>fun write(bytes: ByteArray, ignoreMtu: Boolean) = 
      server.deviceList()
              .first(emptyList())
              .flatMapObservable { devices -&gt;
                  Single.fromCallable {
                      if (ignoreMtu) {
                          bytes.size
                      } else {
                          devices.minBy { device -&gt; device.mtu }?.mtu ?: DEFAULT_MTU
                      }
                  }
                          .flatMapObservable { minMtu -&gt;
                              Observable.fromIterable(bytes.asIterable())
                                      .buffer(minMtu)
                          }
                          .map { it.toByteArray() }
                          .doOnNext { server.currentData = bytes }
                          .map { devices }
                          // part i've left out: waiting for each device acknowledging the message, timeouts, etc.
              }
  </code></pre>
  
  <p>What's missing in here is the part where I only allow <code>one</code> piece of information to be sent at the same time. Also, what I require is that if I'm adding a message into my queue, I have to be able to observe the status of only this message (completed, error). </p>
  
  <p>I've thought about what's the most elegant way to achieve this. Solutions I've came up with include for example a <code>PublishSubject&lt;ByteArray&gt;</code> in which I push the messages (queue-like), add a subscriber and observe it - but this would throw for example <code>onError</code> if the previous message failed.</p>
  
  <p>Another way that crossed my mind was to give each message a number upon creating / queueing it, and have a global "message-counter" Observable which I'd hook into the chain's beginning with a <code>filter</code> for the <code>currently sent message == MY_MESSAGE_ID</code>. But this feels kind of fragile. I could increment the counter whenever the subscription terminates, but I'm sure there must be a better way to achieve my goal.</p>
  
  <p>Thanks for your help.</p>