Original post

I am trying to implement a program such that a websocket connection is maintained between the server and the client.

There are many types of messages that can be sent from the client to the server. For example, in a game, a message from the client could be that they typed something into the in-game chat, or maybe they have changed the server settings, maybe their position has changed, or maybe they want to kick manage the state of another player (e.g. kick them or make them admin).

I have REST APIS in my head, where the client could send requests to different endpoints such as “www.example.com/changeposition” and in the json body you would have your arguments. On the server, in the function handling requests to this endpoint you could assume the structure of the json in the request.

But in a websocket, all the communication from the client comes from one endpoint. So how should we route the messages from the client. Is there a standard thing to do in this situation? Should add a “messagetype” field in the json of every message and switch based on that, like this:

switch request.messageType {
case "move":
case "chat":

Am I wrong to use websockets in this way? Is there a better way to do things?