What doesn't work?
Sending a chat message immediately after the WebSocket connection opens can fail with an internal server error.
The WebSocket is already open from the client’s perspective, but the backend may not have finished initializing client.user yet. If the client sends a clanMessage before client.user is set, the backend throws:
TypeError: Cannot read properties of undefined (reading 'clanId')
What is the endpoint and method? (if applicable)
Endpoint: ws://localhost:8080/ws/chat
Method: WebSocket event clanMessage
How can it be reproduced?
- Start the backend locally.
- Sign in and get a valid JWT token.
- Open a WebSocket connection to
ws://localhost:8080/ws/chat using the token.
- Send a
clanMessage immediately in the client’s open handler, without waiting.
- The server may respond with
Internal server error and log a TypeError because client.user is still undefined.
How should it be fixed?
The backend should not handle chat events before the WebSocket user context has been initialized.
Suggested changes:
- Add an explicit guard/check at the beginning of chat event handlers such as
handleClanMessage, handleGlobalMessage, and reaction handlers:
if (!client.user) {
throw new WsException('WebSocket user is not initialized yet');
}
-
Return a controlled WebSocket error instead of allowing a TypeError.
-
Optionally send a ready event to the client at the end of handleConnection(), after the player has been loaded and client.user has been assigned. The client can then wait for this event before sending chat messages.
Example:
{
"event": "ready",
"data": true
}
This removes the need for arbitrary client-side delays and makes the chat protocol deterministic.
Extra:
The following flow can be used to send a chat message in Powershell:
-
$body = @{ username = "--username--" ; password = "--password--" } | ConvertTo-Json
`
-
$res = Invoke-RestMethod -Method Post -Uri "http://localhost:8080/auth/signIn" -ContentType "application/json" -Body $body
-
send-chat.js:
const WebSocket = require("ws");
const token = process.argv[2];
if (!token) {
console.error("Token puuttuu. Aja: node .\\send-chat.js SINUN_TOKEN");
process.exit(1);
}
const sock = new WebSocket("ws://localhost:8080/ws/chat", {
headers: {
Authorization: "Bearer " + token,
},
});
sock.on("open", () => {
console.log("connected");
setTimeout(() => {
sock.send(JSON.stringify({
event: "clanMessage",
data: {
content: "Testiviesti lokaaliymparistosta",
},
}));
}, 1000);
setTimeout(() => {
sock.close();
}, 3000);
});
sock.on("message", (message) => {
console.log(message.toString());
});
sock.on("error", (error) => {
console.error(error);
});
sock.on("close", () => {
console.log("closed");
});
node .\send-chat.js $env:TOKEN
Please don't include send-chat.js into commits to repo.
What doesn't work?
Sending a chat message immediately after the WebSocket connection opens can fail with an internal server error.
The WebSocket is already open from the client’s perspective, but the backend may not have finished initializing
client.useryet. If the client sends aclanMessagebeforeclient.useris set, the backend throws:What is the endpoint and method? (if applicable)
Endpoint:
ws://localhost:8080/ws/chatMethod: WebSocket event
clanMessageHow can it be reproduced?
ws://localhost:8080/ws/chatusing the token.clanMessageimmediately in the client’sopenhandler, without waiting.Internal server errorand log aTypeErrorbecauseclient.useris still undefined.How should it be fixed?
The backend should not handle chat events before the WebSocket user context has been initialized.
Suggested changes:
handleClanMessage,handleGlobalMessage, and reaction handlers:Return a controlled WebSocket error instead of allowing a
TypeError.Optionally send a
readyevent to the client at the end ofhandleConnection(), after the player has been loaded andclient.userhas been assigned. The client can then wait for this event before sending chat messages.Example:
{ "event": "ready", "data": true }This removes the need for arbitrary client-side delays and makes the chat protocol deterministic.
Extra:
The following flow can be used to send a chat message in Powershell:
$body = @{ username = "--username--" ; password = "--password--" } | ConvertTo-Json`
$res = Invoke-RestMethod -Method Post -Uri "http://localhost:8080/auth/signIn" -ContentType "application/json" -Body $bodysend-chat.js:node .\send-chat.js $env:TOKENPlease don't include
send-chat.jsinto commits to repo.