3

So far I have successfully used the combination of Autowire and uPickle to make Ajax calls on a Spray router - see here for an example. Using this approach there is no blocking because the result returned to the client/browser is a Scala Future - basically the browser application will be informed about the result in due course, whether failure or a 'big list of items' has come back.

The problem I have is that the browser gets the whole 'big list of items' result returned all at once. I would like to do a query that returns an Observable, or a Stream. This will enable the UI to be dynamically populated as the user is watching. In this case the client will be informed as every item comes back (or every few items, it will be up to the client to decide how it interacts with the data 'pipe').

What is the simplest approach to take to enable this streaming into the client in a Scala.js application?

Edit This question has two sides so to make it simpler an answer outlining the code for just the client side will be accepted. The server-side can always be another SO question...

Chris Murphy
  • 6,411
  • 1
  • 24
  • 42

1 Answers1

2

In principle, it seems like the idiomatically best approach is to use Websockets, but that requires Websocket support on the server. IIRC, Spray doesn't have Websockets, but they've recently been added to akka-http, which is the replacement for Spray.

Short of that, it could probably be done using a technique like this one, but it's not exactly elegant. I suspect it wouldn't be hard to write a Scala.js shell around this technique, though.

Note that this question really has little to do with Scala.js per se. SJS can do exactly what JavaScript can do. The crucial question is really whether there is a good way to do this from JavaScript that fits your needs.

That said, Autowire is probably wrong for this purpose -- it's an RPC mechanism, which means that by definition it always returns a complete result. It's not well-suited to streaming, at least yet. This might conceivably change in the future, if we figure out how to adapt Autowire to Akka Streams such that an Autowire call could return a Stream, but we've barely begun to discuss what that would mean yet -- true Stream support is pretty new even on the server, and still under discussion for client-server use.

Community
  • 1
  • 1
Justin du Coeur
  • 2,699
  • 1
  • 14
  • 19
  • So upgrading to akka-http will give Akka Streams? And on the client RxJS could be used? As you mention its not a big deal to use JavaScript from Scala.js. – Chris Murphy Aug 21 '15 at 16:28
  • 1
    Mind, I'm not an expert on this topic -- I'm not upgrading to akka-http myself until it gets integrated into Play. But my understanding is that akka-http is built on top of Akka Streams, so I'm pretty sure the answer to the first is yes. And using that on server, I believe you should be able to choose whatever Webserver lib you like on the client. (Might need to write a facade or two, but that's not hard.) – Justin du Coeur Aug 21 '15 at 17:14
  • I should also add the usual caveat for akka-http: while I gather that it's functioning pretty well, it has *not* been optimized yet. If performance is crucial, note that it's still a lot slower than Spray. – Justin du Coeur Aug 21 '15 at 17:16