3

We are building a websocket server via golang+gin+json+gorilla websocket to push messages from server side to browser.

We plan to provide frontend with some subscription command, which means messages from server side will be sent to those users who subscribed target topic.

My confusion is whether we need add Ack mechanism here? For example, when client subscribe one topic, the server saved this mapping: user --> topic.

Is it necessary for the server to send a response for each subscription request to clients (Like that we do for an RPC request)? And how to do that? Below is my consumption

type MsgHeader struct {
    ReqId string  `json: reqId`
    Cmd   string  `json: cmd`

    // either of "req" or "rsp"
    // is it necessary to have this field???
    Type  string  `json: type`            
}

I mean the application level acknowledgement, like what we do for RPC requests. For RPC request, we send responses even when the response itself is empty, something like:

type SubscriptionRsp struct {
     Code int
     Msg  string
     Data interface{}
}
Wallace
  • 561
  • 2
  • 21
  • 54
  • What kind of Ack? If you mean simply packet/message acknowledgement, that is already built into TCP, which underlies websockets. – Hymns For Disco Jun 16 '21 at 22:18
  • I mean application level acknowledgement, like what we do for RPC request. For RPC request, we send response even when body is nil, to tell users if the subscription is successful @HymnsForDisco – Wallace Jun 16 '21 at 22:20
  • 1
    Whether or not you need acknowledgement for specific features of your application is dependent on the application itself. It's possible that the client can function correctly without an acknowledgement. It's possible that it can't, or maybe shouldn't. It all depends on how the service and client are designed. – Hymns For Disco Jun 16 '21 at 22:22

1 Answers1

2

No, it's not necessary.

The Websocket specs (RFC 6455) does not mandate this.

A data frame MAY be transmitted by either the client or the server at any time after opening handshake completion and before that endpoint has sent a Close frame

Nothing else about acknowledging messages is said in the Sending and Receiving Data section.

Therefore any ACK is entirely an implementation detail of your application. It may be useful if you develop a resilient client that retries failed messages, where "failed" could a message that is successfully sent to the server but not processed as expected.

blackgreen
  • 34,072
  • 23
  • 111
  • 129