-
Notifications
You must be signed in to change notification settings - Fork 452
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
✨ Add ping/pong #576
✨ Add ping/pong #576
Conversation
4f6a556
to
5cb2dc5
Compare
NB This would be a perfect candidate for a plugin: #337 |
@@ -476,6 +478,12 @@ Connection.prototype.send = function(message) { | |||
this.socket.send(JSON.stringify(message)); | |||
}; | |||
|
|||
Connection.prototype.ping = function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From discussion today - let's throw an error if the connection state is not connected.
This is to guard against a bunch of ping requests queueing up at the socket layer if the socket is currently disconnected or connecting, and having them suddenly flood the socket when eventually connected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done — added ERR_CANNOT_PING_OFFLINE
This change adds the ability for a client to send a "ping" to the server and receive a "pong" response. The motivation for this is that sometimes the socket underlying the `Connection` may not reliably report its connection state (for example if using a wrapper around a "vanilla" websocket that handles reconnection). The most bullet-proof way for a client to determine its connection state is to actually make a request to the server and assert that it receives a timely response. The implementation of ping/pong is arguably a websocket concern, especially since it's already [defined by the spec][1]. However, the browser JavaScript [`WebSocket`][2] does not expose this functionality, so we have to add our own ping/pong layer on top anyway. It's also worth noting that consumers of this library can't easily send their own ping messages down the socket, since ShareDB will [error][3] for unknown messages. Note that this change only adds the ability for a client to ping the server, and not the other way around. This is because: - the `Agent` is not an `Emitter`, and emitting a `pong` on the `Backend` is pretty meaningless - server-side implementations of WebSockets, such as `ws`, _do_ [expose a `ping` API][4] [1]: https://www.rfc-editor.org/rfc/rfc6455#section-5.5.2 [2]: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket [3]: https://github.com/share/sharedb/blob/8b531bedf19860ffe0b37a3e1c8da7a32b5005bd/lib/agent.js#L451 [4]: https://github.com/websockets/ws/blob/966f9d47cd0ff5aa9db0b2aa262f9819d3f4d414/lib/websocket.js#L351
5cb2dc5
to
56ac7a8
Compare
Very nice! |
This change adds the ability for a client to send a "ping" to the server
and receive a "pong" response.
The motivation for this is that sometimes the socket underlying the
Connection
may not reliably report its connection state (for exampleif using a wrapper around a "vanilla" websocket that handles
reconnection).
The most bullet-proof way for a client to determine its connection state
is to actually make a request to the server and assert that it receives
a timely response.
The implementation of ping/pong is arguably a websocket concern,
especially since it's already defined by the spec. However, the
browser JavaScript
WebSocket
does not expose this functionality,so we have to add our own ping/pong layer on top anyway.
It's also worth noting that consumers of this library can't easily
send their own ping messages down the socket, since ShareDB will
error for unknown messages.
Note that this change only adds the ability for a client to ping the
server, and not the other way around. This is because:
Agent
is not anEmitter
, and emitting apong
on theBackend
is pretty meaninglessws
, doexpose a
ping
API