Lagom 1.4.8 - Receive a Source[ByteString] for MessageSerializer


(Rui Oliveira) #1

Hi ,

We have a use case where we are uploading large files using the Lagom, our current implementation is using a custom StrictMessageSerializer that only returns the ByteString it self, something like:

val ByteStringMessageSerializer: StrictMessageSerializer[ByteString] = new StrictMessageSerializer[ByteString] {
    override def serializerForRequest = new NegotiatedSerializer[ByteString, ByteString] {
      override def serialize(message: ByteString): ByteString = message
    }

    override def deserializer(messageProtocol: MessageProtocol) = new NegotiatedDeserializer[ByteString, ByteString] {
      override def deserialize(wire: ByteString) = wire
    }

    override def serializerForResponse(acceptedMessageProtocols: immutable.Seq[MessageProtocol]) = new NegotiatedSerializer[ByteString, ByteString] {
      override def serialize(message: ByteString): ByteString = message
    }
}

But this solution can’t scale, since we are putting all the file in memory.

Is there any way of having a: Source[ByteString, NotUsed] ?
I investigated about the StreamedMessageSerializer but I think it is for web-sockets only.


(Sergey Morgunov) #2

May be this recipe can help you https://github.com/lagom/lagom-recipes/tree/master/file-upload/file-upload-scala-sbt.
Plus FilePartHandler https://www.playframework.com/documentation/2.7.x/ScalaFileUpload#Writing-your-own-body-parser


(Rui Oliveira) #3

Hi @ihostage,

Thanks, after futher investigation we was able to implement it using PlayServiceCall.

Now i have other question:
When i do requests to a service using lagom Service client we have the same problem: we put everthing in memory and we send the request… is there any way to make it work with a Stream of a ByteString?

Thanks in advance!