-
Notifications
You must be signed in to change notification settings - Fork 25k
Add ping to WebSocket #8505
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 to WebSocket #8505
Conversation
|
By analyzing the blame information on this pull request, we identified @philikon and @nicklockwood to be potential reviewers. |
Libraries/WebSocket/WebSocket.js
Outdated
| throw new Error('Unsupported data type'); | ||
| } | ||
|
|
||
| sendPing(): void { |
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.
This is not part of the WebSocket spec, right? How do other libs implement it?
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.
It's not part of the JS browser API. Most low level WebSocket implementations expose a ping or sendPing method. Usually you can add data to the ping that will be sent back as a pong by the server.
Higher level libs like socket.io do not directly expose a method for pinging but instead manages the regular pinging internally: https://github.com/socketio/engine.io-client/blob/2c55b278a491bf45313ecc0825cf800e2f7ff5c1/lib/socket.js#L487
|
It looks like something which can be implemented on JS side on top of existing APIs. I think we don't need to have this in core. Let me know if I'm wrong. |
|
It's not possible to do this in JS. WebSocket pings are control frames with a different op-code than messages (see rfc). The underlying libraries already contain methods for ping, close and pong, the three control frames currently implemented, but only close is exposed in JS. You could of course send regular messages to the server to keep the connection open, but ping frames are tailor made for this. Regular messages would potentially require changes in the server component to handle these messages. Ping control frames are handled by all WebSocket implementations I know of. For me this is not really an alternative. If you really don't want this in core, maybe we could make the Android implementation of WebSocket extendable? For iOS we already released an app with ping capability, with no changes to core. I put a category on RCTWebSocketModule adding the sendPing method and extended the JS WebSocket module, adding sendPing there as well. With Android I would have to use reflection or copy the existing implementation since |
I see. Thanks for the link.
I've nothing against adding it to core. Was just checking if it's possible without adding an API. Since it's a control frame like |
It seems that RCTSRWebSocket already handles that automatically: https://github.com/facebook/react-native/blob/master/Libraries/WebSocket/RCTSRWebSocket.m#L712-L720. okhttp3 does as well: https://github.com/square/okhttp/blob/master/okhttp-ws/src/main/java/okhttp3/internal/ws/RealWebSocket.java#L65-L74 As they should IMHO. The ping/pong messages seem like an implementation detail of the protocol. I'm not 100% sure we should even expose sending pings, rather than doing it automatically for the user (which is what browsers seem to do), but adding it can't hurt for now. I agree that we should call it |
|
@dbasedow Let's rename the native methods as well. |
|
I changed the name to ping in the native modules as well. |
|
@facebook-github-bot shipit |
|
Thanks for importing. If you are an FB employee go to Phabricator to review. |
|
FWIW, unless I'm misreading the Firefox source, it seems that at least Firefox isn't sending pings by default. (And I'm not even sure it's sending them on a regular basis.) I couldn't be bothered to test this, though ;). Anyway, that makes a strong case for providing the API so that consumers can do something with ping if they want to. |
Summary:
Idle WebSocket connections get reset after a few minutes of inactivity. To prevent this, most WebSocket implementations offer sending special ping messages. This PR adds a method `sendPing()` to WebSocket. Ping payloads are not supported.
Manual testing can be done by adding `connection.on('ping', _ => console.log('Received ping'));` to a ws connection or using a packet sniffer while sending pings.
Closes facebook#8505
Differential Revision: D3516260
Pulled By: dmmiller
fbshipit-source-id: cfebf5899188ae53254d5be6b666a9075e0eed89
Summary:
Idle WebSocket connections get reset after a few minutes of inactivity. To prevent this, most WebSocket implementations offer sending special ping messages. This PR adds a method `sendPing()` to WebSocket. Ping payloads are not supported.
Manual testing can be done by adding `connection.on('ping', _ => console.log('Received ping'));` to a ws connection or using a packet sniffer while sending pings.
Closes facebook#8505
Differential Revision: D3516260
Pulled By: dmmiller
fbshipit-source-id: cfebf5899188ae53254d5be6b666a9075e0eed89
|
curious why the pong response is not exposed as an event to the bridge? should we consider adding this? /cc @dbasedow |
Summary:
Idle WebSocket connections get reset after a few minutes of inactivity. To prevent this, most WebSocket implementations offer sending special ping messages. This PR adds a method `sendPing()` to WebSocket. Ping payloads are not supported.
Manual testing can be done by adding `connection.on('ping', _ => console.log('Received ping'));` to a ws connection or using a packet sniffer while sending pings.
Closes facebook/react-native#8505
Differential Revision: D3516260
Pulled By: dmmiller
fbshipit-source-id: cfebf5899188ae53254d5be6b666a9075e0eed89
Idle WebSocket connections get reset after a few minutes of inactivity. To prevent this, most WebSocket implementations offer sending special ping messages. This PR adds a method
sendPing()to WebSocket. Ping payloads are not supported.Manual testing can be done by adding
connection.on('ping', _ => console.log('Received ping'));to a ws connection or using a packet sniffer while sending pings.