robtaussig / react-use-websocket

React Hook for WebSocket communication
MIT License
1.62k stars 135 forks source link

feat: allow ping message to be dynamically created #226

Closed michaelboyles closed 8 months ago

michaelboyles commented 9 months ago

This change allows heartbeat.message to accept a function which returns a string

My use-case is to send a ping which includes the send time from the client's perspective

{"_type": "ping", "timestamp": "2016-06-03T23:15:33.008Z"}

e.g.

useWebsocket(url, {
    heartbeat: {
        message: () => JSON.stringify({ type: 'ping', timestamp: currentTime() })
    }
}
robtaussig commented 8 months ago

@michaelboyles Released in 4.8.0!