Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] Jetty 9 websocket ping/pong messages

Don't forget to also handle javax.websocket @OnError

http://docs.oracle.com/javaee/7/api/javax/websocket/OnError.html


--
Joakim Erdfelt <joakim@xxxxxxxxxxx>
Expert advice, services and support from from the Jetty & CometD experts


On Tue, Jul 29, 2014 at 11:11 AM, Manoj Khangaonkar <Manoj.Khangaonkar@xxxxxxxxxx> wrote:
Hi,

So it is appears not worthwhile for the application layer to try to use ping/pong.

SetMaxIdleTimeout can be used to keep connections alive for the specified time. 

But connections can get closed for other reasons like loss of network connection. It appears that the best strategy for both
The server and the client is to be aware of connection close ( OnClose called by jetty) and handle it gracefully. In the
Case of the client , the client can reconnect when required. In the case of the server , it has to discard the current connection 
And wait for the client to connect again. 

If the connection fails while the server is trying to write to the client, I assume an IOException or some Exception will be thrown,
The server can reattempt the write when the client makes a connection the next time.

If there are other better ways to do connection management, please let me know.

regards

From: Joakim Erdfelt <joakim@xxxxxxxxxxx>
Reply-To: JETTY user mailing list <jetty-users@xxxxxxxxxxx>
Date: Monday, July 28, 2014 at 5:51 PM
To: JETTY user mailing list <jetty-users@xxxxxxxxxxx>
Subject: Re: [jetty-users] Jetty 9 websocket ping/pong messages

PING / PONG on websocket is considered a back-channel.  A form of health check.
Its supposed to be automatically responded to (similar to ICMP echo, SPDY ping, HTTP/2 ping, etc...).

Many implementations do not support sending a pong.
This is because sending a pong with a different payload (something that becomes possible when you expose .sendPong()) is often interpreted as a failed PING/PONG pair.

The jetty exposure is because of JSR-356, aka javax.websocket.  (This API has many small issues.  the next iteration will hopefully soften the numerous papercuts present)
Know this, Jetty will *ALWAYS* respond to a PING it receives with a PONG of the same payload (as soon as it has parsed the PING, it shoves the PONG at the front of the frame queue for outgoing reply)
Therefore, using javax.websocket.RemoteEndpoint.sendPong() will result in a naked pong for a unsent ping.

The PING/PONG is also out-of-band, meaning it can exist in the middle of other messages.
Since the APIs for notifications are supposed to be thread-safe, this out of band behavior of websocket control messages means that if you were handling a large message, consisting of many websocket frames (fragments), we couldn't notify you of the PING or PONG until your endpoint is done handling the message. (this is especially true of Decoded or Stream based message handling).

As for keeping a connection alive, that's a facility of idle timeout.
If you want to track latency, send a PING with a sequence indicator + timestamp.  in your onPong, compare the sequence indicator (if desired), and the timestamp for latency.  Also know that the latency you see here can be skewed by a large frame. (if a frame is in progress, nothing else can be sent until that frame is completed).
If you want to discover network issues faster (such as a mobile device going out of coverage), then use a PING/PONG, the result will be a non-response, or have ridiculous latency, or even cause an IO error with an abnormal close.


--
Joakim Erdfelt <joakim@xxxxxxxxxxx>
Expert advice, services and support from from the Jetty & CometD experts


On Mon, Jul 28, 2014 at 4:31 PM, Manoj Khangaonkar <Manoj.Khangaonkar@xxxxxxxxxx> wrote:
Hi,

RemoteEndPoint class has sendPing and sendPong methods.

If the server or client sends a ping, how is the other end expected to handle the message and send a pong. In other words ,
Which method gets called on receipt of a ping.

If you have used WebSocketListener or WebSocketAdapter, is it the OnWebSocketBinary method that gets called ?

If you have the @WebSocket annotation, Will it be the method annotated with @WebSocketFrame or the one annotated with @WebSocketMessage.

Since clients could use other APIs or languages that do not expose ping/pong, is there a recommendation on the best way to do a
Heartbeat to keep connections alive.

regards
This e-mail and any attached files are intended solely for the use of the individual or entity to which this mail is addressed and may contain information that is privileged, confidential and exempt from disclosure under applicable law. Any use, disclosure, copying or distribution of this e-mail or the attached files by anyone other than the intended recipient is strictly prohibited. If you have received this e-mail in error, please notify the sender by reply e-mail or collect call to (650) 388-4111 and delete this e-mail and attached files from your system. Thank you.

_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-users

This e-mail and any attached files are intended solely for the use of the individual or entity to which this mail is addressed and may contain information that is privileged, confidential and exempt from disclosure under applicable law. Any use, disclosure, copying or distribution of this e-mail or the attached files by anyone other than the intended recipient is strictly prohibited. If you have received this e-mail in error, please notify the sender by reply e-mail or collect call to (650) 388-4111 and delete this e-mail and attached files from your system. Thank you.

_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-users


Back to the top