Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] Emit ping via WriteMessage() or WriteControl()? #652

Closed
tomsun opened this issue Nov 19, 2020 · 6 comments
Closed

[question] Emit ping via WriteMessage() or WriteControl()? #652

tomsun opened this issue Nov 19, 2020 · 6 comments
Labels

Comments

@tomsun
Copy link

tomsun commented Nov 19, 2020

Are both idiomatic?

What are the trade-offs?


Chat example emits ping using WriteMessage()

if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil {

Command example emits ping using WriteControl()

if err := ws.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(writeWait)); err != nil {

@tomsun
Copy link
Author

tomsun commented Nov 19, 2020

Assuming both are ok, is WriteControl() safer concurrency-wise: for example if you don't want to couple ping and the writer of regular messages? Or is that a bad idea?

Applications are responsible for ensuring that no more than one goroutine calls the write methods ( ... WriteMessage ... )

The Close and WriteControl methods can be called concurrently with all other methods.

https://pkg.go.dev/github.com/gorilla/websocket#hdr-Concurrency

@tomsun tomsun changed the title [question] Emit ping via WriteMessage() or WriteControl() [question] Emit ping via WriteMessage() or WriteControl()? Nov 19, 2020
@ghost
Copy link

ghost commented Nov 19, 2020

WriteControl can be called concurrently with other write methods. WriteMessage cannot. Control messages (ping is a control message) can be written with both methods.

In the chat example, all application writes to the connection are from a single goroutine. Because concurrency is not an issue here, either method can be used.

The command example writes ping messages from a different goroutine than the goroutine that writes data messages. WriteControl or some form of synchronization is required in the example.

WriteControl can write a message concurrently with WriteMessage because control messages can be interleaved with a data message's frames in the websocket protocol.

There's no downside to calling WriteControl for all control messages.

@tomsun
Copy link
Author

tomsun commented Nov 24, 2020

@TaylorRex ok, thanks for the confirmation!

So I should be able to use WriteControl() if I can't structure my app to ping from the same goroutine. But I give up the ability to use things like SetWriteDeadline() for the ping message itself, like in the chat example.

@ghost
Copy link

ghost commented Nov 24, 2020

But I give up the ability to use things like SetWriteDeadline()

Use the WriteControl deadline argument to specify a deadline for writes using the method.

@tomsun
Copy link
Author

tomsun commented Dec 2, 2020

Oh, perfect!

@kuchaguangjie
Copy link

kuchaguangjie commented Feb 13, 2022

WriteControl can be called concurrently with other write methods. WriteMessage cannot.

What exactly does this mean?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants