Arif Terzioğlu
05/19/2025, 12:32 PMinstall(WebSockets) {
contentConverter = KotlinxWebsocketSerializationConverter(Json)
pingPeriod = Duration.ofSeconds(10)
timeout = Duration.ofSeconds(45)
maxFrameSize = Long.MAX_VALUE
masking = false
}
import io.ktor.server.websocket.webSocket
webSocket ("/api/public/orders/{orderId}/live-updates/v1") {
//...
}
React Client config
heartbeat: {
message: 'ping',
returnMessage: 'pong',
timeout: 30000, // 30 seconds, if no response is received, the connection will be closed
interval: 20000 // every 20 seconds, a ping message will be sent
}
Response from the browserAleksei Tirman [JB]
05/19/2025, 12:39 PMArif Terzioğlu
05/19/2025, 12:44 PMAleksei Tirman [JB]
05/19/2025, 12:45 PMArif Terzioğlu
05/19/2025, 12:47 PMArif Terzioğlu
05/19/2025, 12:47 PMIf the heartbeat option is set to true or has additional options, the library will send a 'ping' message to the server every interval milliseconds. If no response is received within timeout milliseconds, indicating a potential connection issue, the library will close the connection. You can customize the 'ping' message by changing the message property in the heartbeat object. If a returnMessage is defined, it will be ignored so that it won't be set as the lastMessage.
const { sendMessage, lastMessage, readyState } = useWebSocket(
'<ws://localhost:3000>',
{
heartbeat: {
message: 'ping',
returnMessage: 'pong',
timeout: 60000, // 1 minute, if no response is received, the connection will be closed
interval: 25000, // every 25 seconds, a ping message will be sent
},
}
);
Aleksei Tirman [JB]
05/19/2025, 12:49 PM