# WebSocket Heartbeat

**URL:** <https://discourse.julialang.org/t/websocket-heartbeat/54291>\
**Category:** Web Stack\
**Tags:** question\
**Created:** [January 30, 2021, 7:04pm UTC](https://discourse.julialang.org/t/websocket-heartbeat/54291 "2021-01-30T19:04:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MasFlam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/masflam/32/20950_2.png) [@MasFlam](https://discourse.julialang.org/u/MasFlam)\
**Post date:** [January 30, 2021, 7:04pm UTC](https://discourse.julialang.org/t/websocket-heartbeat/54291/1 "2021-01-30T19:04:31Z")

</div>

The websockets Discord API expects clients to send a “heartbeat” every X milliseconds (it’s around a minute) to keep the connection alive. How can I (using WebSockets.jl or the `HTTP.WebSockets` module) send a websocket message containing the heartbeat in fixed intervals while still receiving events and also sometimes sending other messages?

---

<div class="post-metadata">

**Author:** ![Zentrik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zentrik/32/35409_2.png) [@Zentrik](https://discourse.julialang.org/u/Zentrik)\
**Post date:** [February 6, 2023, 10:43pm UTC](https://discourse.julialang.org/t/websocket-heartbeat/54291/2 "2023-02-06T22:43:07Z")

</div>

Did you end up figuring this out? Thanks

---

<div class="post-metadata">

**Author:** ![Zentrik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zentrik/32/35409_2.png) [@Zentrik](https://discourse.julialang.org/u/Zentrik)\
**Post date:** [February 8, 2023, 10:41pm UTC](https://discourse.julialang.org/t/websocket-heartbeat/54291/3 "2023-02-08T22:41:05Z")

</div>

```julia
using HTTP.WebSockets

uri = "wss://bla.bla"
heartbeatJSON = ""

WebSockets.open(uri) do socket
    @sync begin
		@async for msg in socket
			println(msg)
		end

		@async begin
			sleep(30)
			while !WebSockets.isclosed(socket)
				send(socket, heartbeatJSON)
				sleep(30)
			end
		end
end

```

This should work.
