0

I have a web socket proxy that accepts messages and passes them through to clients (browser and Flash).

In an attempt to optimize I was hoping to GZIP the data that goes over that web socket connection. Is this possible, and/or what are the other approaches that might work for this?

I know that there is a WebSocket extension being worked on according to this StockOverflow question.

My current approach within a Scala/Jetty application:

def compressBytes(bytes:Array[Byte]) = {
    val bos = new ByteArrayOutputStream
    val gzip = new GZIPOutputStream(bos)
    gzip.write(bytes)
    gzip.close
    bos.toByteArray
  }

sent to the client:

def onMessage(bytes:Array[Byte], offset:Int, length:Int) {
  serverSocket.connection.sendMessage(compressBytes(bytes), offset, length)
}

Side note: I know that the Sec-WebSocket-Extensions: permessage-deflate is a possibility, but not yet full adopted (Jetty 9 has it I believe)

Thanks

Community
  • 1
  • 1
binarygiant
  • 6,362
  • 10
  • 50
  • 73

1 Answers1

0

With jetty, you can just drop in a servlet filter to do the job: http://www.eclipse.org/jetty/documentation/current/gzip-filter.html

Kevin Wright
  • 49,540
  • 9
  • 105
  • 155
  • Thanks, I have dropped that filter in, but I didn't see any difference in the message size (wiresshark/tcpdump). Then I thought I might need to pass something in the websocket handshake (accept-encoding: gzip, deflate, or the Sec-WebSockets-Extensions header). Any thoughts on how to force Jetty to gzip the web socket traffic? – binarygiant Feb 12 '14 at 17:38
  • Filter is GzipFilter org.eclipse.jetty.servlets.GzipFilter mimeTypes text/html,text/plain,text/xml,application/xhtml+xml,text/css,application/javascript,image/svg+xml – binarygiant Feb 12 '14 at 17:42