0

Suppose I have a server and a client. When I send a message to the client, would I be sending a ServerMessage, or a ClientMessage? The other way to think about it - when I receive a message, it is a ClientMessage or a ServerMessage? Or is it ClientMessage in both cases?!

3
  • 6
    Maybe it's just a Message in all cases. Does it need to be different? Commented Feb 10, 2019 at 22:29
  • Some places speak of servers sending down-stream messages to clients and clients sending up-stream messages to the server. However, Josph is right, both are messages. Commented Feb 11, 2019 at 6:51
  • 1
    Given there are only two parties in involved in the communication, and a single message can only go one way, distinguishing either the originator or the recipient seems redundant. Call it a message. Commented Feb 11, 2019 at 10:14

5 Answers 5

6

If possible, use a different naming scheme that allows for more meaningful and less ambiguous names. Then choose names so that they reflect what the message is (or what kind of data it contains), rather then who the sender/recipient is. You could then document the kinds of messages each party can send.

Depending on the domain, the names themselves, if chosen well, may be able to implicitly indicate the originator - e.g., the business logic may be such that a certain kind of message can only be sent by, say, the server.

If you need to disambiguate who the sender is in documentation, then you can use a clarifying phrase, e.g. "client-sent message", or "server-sent message". If you need to clarify at which end some processing happens, use "client-side" and "server-side".

Alternatively, assuming an active client and passive (reactive) server, you can use the request/response terminology. The client always sends requests, while the server always returns responses.

As for "client message" / "server message", while you could use that, as you yourself have noticed, different people could interpret that in different ways, and that could be a source of unnecessary confusion. You'd have to pick one of the two meanings, document your choice somewhere ("within this project, client message means [...], and server message means [...]"), and you'd have to make sure that these terms are used consistently throughout - but that may prove difficult, especially if new people join the project.

2
  • This answer sort of avoids the question, which is, what is a good collective name for messages going from the client to the server and vice versa. This pops up all the time when you for example have commonalities between all client-to-server messages and need a name for a base class of such messages. ServerToClientMessage and ClientToServerMessage are the best alternatives I've come up with. Long (unfortunately) but quite clear. Commented Jul 8, 2024 at 6:49
  • @aioobe No, this answer rejects the premise of the question. There's a difference. I'm not addressing the question explicitly asked, but the flaw in the underlying thinking. What you propose is an obvious enough way to be clear about directionality, I just don't think directionality is the most important thing to communicate (in the sense that the name is still very generic and doesn't tell you what the class brings to the table, and the convention starts to break as soon as there's a need to have more than one of those). It smells of an ad hoc APIs, and by extension, system. Commented Jul 8, 2024 at 21:12
3

How about making the names more descriptive?

MessageToClient

MessageToServer

That way it's clear where it goes.

1

Ordinarily, though this does somewhat depend on the protocol, I would think a client (or whatever's originating the connection) would send a request, which the server would receive and then answer with a response. The contents of either would be a body, and (again, depending on the protocol) metadata could be sent in a header or footer.

0

Name the messages sent from the server to the client "S2CMessages" and the messages sent in the other direction "C2SMessages". That way there is no confusion about which way the message flows.

2
  • 4
    You have swapped ambiguous names for cryptic ones. This is not an improvement. Commented Feb 11, 2019 at 8:10
  • @DavidArno What's cryptic about S2C and C2S? AFAICT they are pretty usual terms in game networking. Commented Feb 11, 2019 at 21:14
0

It's pretty ambiguous. Ultimately, it's defined by your context:

  • Strict source-centric context: "Where did the message come from?" A server sends a ServerMessage, which is received by the client as a ServerMessage. The client sends a ClientMessage, which is received by the server as a ClientMessage.

  • Strict destination-centric context: "Where is this message going?". A server sends a ClientMessage, which is received by the client as a ClientMessage. The client sends a ServerMessage, which is received by the server as a ServerMessage.

  • Source/Destination-centric context: "Is this message from me?" A server sends a ClientMessage, which is received by the client as a ServerMessage. The client sends a ServerMessage, which is received by the server as a ClientMessage.

So it can be really ambiguous - somebody reading the code won't really know what context is being used. This is the reason why other authors are suggesting that the naming convention is changed (such as MessageToClient or MessageToServer).

My personal preference is to just call it a Message.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.