2 added 4 characters in body
source | link

Protobuf is nice and dandy, but it was not made with self-description in mind. Now this is totally okay if you're using a well-defined protocol and want to replace e.g. SOAP-messages or just want to replace a restful JSON API or the like.

However I would like to use it over a Websocket though and instead of closing and reopening it up again and again under different urls - which defeats the point of the websocket - I would like to keep it open and send different messages over the wire as Byte-arrays!

Now my problem is simple yet a bit complex to solve. I want to send an arbitrary message from client to the server (or vice versa) and the receiver should simple determine which type of message it got and how to interpret it.

In pseudocode this would look like so:

client:

Message m = new Auth().withUserName("Sorona").withPassword("TotallyNotMyActualPassword") 

ws.send(m)

ws.send(m)

server:

Map[Type, Handler]

receiveMessage(Message m) {
   handlers.get(m.determineType()).handle(m)
}

handler:

trait Handler[T] {
  def handle()
}

So what I truly want is two things:

1) Be able to determine the actual type of Any message without (dramatically) increasing the message size.

2) Be robust and extendable

For a new message type I simply want to add a handler and be done.

No hardcoded enums I have to maintain, no switch-case structures I have to adopt etc.

In simple OOP this would be fairly easy but with Protobufs I am kind of stuck.

Any hints?

Protobuf is nice and dandy, but it was not made with self-description in mind. Now this is totally okay if you're using a well-defined protocol and want to replace e.g. SOAP-messages or just want to replace a restful JSON API or the like.

However I would like to use it over a Websocket though and instead of closing and reopening it up again and again under different urls - which defeats the point of the websocket - I would like to keep it open and send different messages over the wire as Byte-arrays!

Now my problem is simple yet a bit complex to solve. I want to send an arbitrary message from client to the server (or vice versa) and the receiver should simple determine which type of message it got and how to interpret it.

In pseudocode this would look like so:

client:

Message m = new Auth().withUserName("Sorona").withPassword("TotallyNotMyActualPassword")

ws.send(m)

server:

Map[Type, Handler]

receiveMessage(Message m) {
   handlers.get(m.determineType()).handle(m)
}

handler:

trait Handler[T] {
  def handle()
}

So what I truly want is two things:

1) Be able to determine the actual type of Any message without (dramatically) increasing the message size.

2) Be robust and extendable

For a new message type I simply want to add a handler and be done.

No hardcoded enums I have to maintain, no switch-case structures I have to adopt etc.

In simple OOP this would be fairly easy but with Protobufs I am kind of stuck.

Any hints?

Protobuf is nice and dandy, but it was not made with self-description in mind. Now this is totally okay if you're using a well-defined protocol and want to replace e.g. SOAP-messages or just want to replace a restful JSON API or the like.

However I would like to use it over a Websocket though and instead of closing and reopening it up again and again under different urls - which defeats the point of the websocket - I would like to keep it open and send different messages over the wire as Byte-arrays!

Now my problem is simple yet a bit complex to solve. I want to send an arbitrary message from client to the server (or vice versa) and the receiver should simple determine which type of message it got and how to interpret it.

In pseudocode this would look like so:

client:

Message m = new Auth().withUserName("Sorona").withPassword("TotallyNotMyActualPassword") 

ws.send(m)

server:

Map[Type, Handler]

receiveMessage(Message m) {
   handlers.get(m.determineType()).handle(m)
}

handler:

trait Handler[T] {
  def handle()
}

So what I truly want is two things:

1) Be able to determine the actual type of Any message without (dramatically) increasing the message size.

2) Be robust and extendable

For a new message type I simply want to add a handler and be done.

No hardcoded enums I have to maintain, no switch-case structures I have to adopt etc.

In simple OOP this would be fairly easy but with Protobufs I am kind of stuck.

Any hints?

1
source | link

Arbitrary protobuf message as byte-array over Websocket - how to determine actual message type upfront

Protobuf is nice and dandy, but it was not made with self-description in mind. Now this is totally okay if you're using a well-defined protocol and want to replace e.g. SOAP-messages or just want to replace a restful JSON API or the like.

However I would like to use it over a Websocket though and instead of closing and reopening it up again and again under different urls - which defeats the point of the websocket - I would like to keep it open and send different messages over the wire as Byte-arrays!

Now my problem is simple yet a bit complex to solve. I want to send an arbitrary message from client to the server (or vice versa) and the receiver should simple determine which type of message it got and how to interpret it.

In pseudocode this would look like so:

client:

Message m = new Auth().withUserName("Sorona").withPassword("TotallyNotMyActualPassword")

ws.send(m)

server:

Map[Type, Handler]

receiveMessage(Message m) {
   handlers.get(m.determineType()).handle(m)
}

handler:

trait Handler[T] {
  def handle()
}

So what I truly want is two things:

1) Be able to determine the actual type of Any message without (dramatically) increasing the message size.

2) Be robust and extendable

For a new message type I simply want to add a handler and be done.

No hardcoded enums I have to maintain, no switch-case structures I have to adopt etc.

In simple OOP this would be fairly easy but with Protobufs I am kind of stuck.

Any hints?