Class SsePushConnection

java.lang.Object
com.vaadin.flow.server.communication.SsePushConnection
All Implemented Interfaces:
PushConnection, Serializable

public class SsePushConnection extends Object implements PushConnection, Serializable
A PushConnection implementation using Server-Sent Events (SSE) without the Atmosphere framework.

SSE is used for server-to-client communication while XHR is used for client-to-server messages (similar to the WEBSOCKET_XHR transport pattern).

For internal use only. May be renamed or removed in a future release.

Since:
24.7
Author:
Vaadin Ltd
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    protected static enum 
    Connection states for SSE push.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an instance connected to the given UI.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    connect(jakarta.servlet.AsyncContext asyncContext)
    Associates this SsePushConnection with the given AsyncContext representing an established SSE connection.
    void
    Called when the connection to the client has been lost.
    void
    Closes the connection.
    protected jakarta.servlet.AsyncContext
    Gets the AsyncContext associated with this connection.
    Returns the state of this connection.
    protected UI
    Gets the UI associated with this connection.
    boolean
    Returns whether this connection is currently open.
    void
    Pushes pending state changes and client RPC calls to the client.
    void
    push(boolean async)
    Pushes pending state changes and client RPC calls to the client.
    protected void
    Sends the given message to the current client using SSE format.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SsePushConnection

      public SsePushConnection(UI ui)
      Creates an instance connected to the given UI.
      Parameters:
      ui - the UI to which this connection belongs
  • Method Details

    • push

      public void push()
      Description copied from interface: PushConnection
      Pushes pending state changes and client RPC calls to the client. Can be called even if PushConnection.isConnected() is false; the push will be deferred until a connection is available. It is NOT safe to invoke this method if not holding the session lock.

      This is internal API; please use UI.push() instead.

      Specified by:
      push in interface PushConnection
    • push

      public void push(boolean async)
      Pushes pending state changes and client RPC calls to the client. If isConnected() is false, defers the push until a connection is established.
      Parameters:
      async - True if this push asynchronously originates from the server, false if it is a response to a client request.
    • sendMessage

      protected void sendMessage(String message)
      Sends the given message to the current client using SSE format. Cannot be called if isConnected() returns false.
      Parameters:
      message - The message to send
    • isConnected

      public boolean isConnected()
      Description copied from interface: PushConnection
      Returns whether this connection is currently open.
      Specified by:
      isConnected in interface PushConnection
      Returns:
      true if the connection is open, false otherwise
    • connect

      public void connect(jakarta.servlet.AsyncContext asyncContext) throws IOException
      Associates this SsePushConnection with the given AsyncContext representing an established SSE connection. If already connected, calls disconnect() first. If there is a deferred push, carries it out via the new connection.
      Parameters:
      asyncContext - the async context to associate this connection with
      Throws:
      IOException - if setting up the SSE connection fails
    • getUI

      protected UI getUI()
      Gets the UI associated with this connection.
      Returns:
      the UI associated with this connection.
    • getAsyncContext

      protected jakarta.servlet.AsyncContext getAsyncContext()
      Gets the AsyncContext associated with this connection.
      Returns:
      The AsyncContext associated with this connection or null if connection not open.
    • disconnect

      public void disconnect()
      Description copied from interface: PushConnection
      Closes the connection. Cannot be called if PushConnection.isConnected() is false.
      Specified by:
      disconnect in interface PushConnection
    • connectionLost

      public void connectionLost()
      Called when the connection to the client has been lost.
    • getState

      protected SsePushConnection.State getState()
      Returns the state of this connection.
      Returns:
      the state of this connection