Class RealtimeSession

java.lang.Object
com.alibaba.dashscope.resources.audio.RealtimeSession
All Implemented Interfaces:
AutoCloseable

public final class RealtimeSession extends Object implements AutoCloseable
Realtime session (audio design §1.3): OpenAI-Realtime-style protocol — pure JSON text frames distinguished by type, audio base64-encoded (a protocol limitation; ~33% size overhead, prefer the task protocol for heavy traffic), no task_id, so every session owns a dedicated socket and cannot share a WsConnection.

Pull model — no SDK threads: OkHttp's reader enqueues events, events() streams them to the caller's blocking thread, consuming each frame after delivery. User discipline is one send thread + one receive thread, finally close().

close() is idempotent and the session may re-connect() afterwards; a generation counter drops late frames from a previous socket so stale events never leak into the new generation (2.x omni parity). endSession() is unsupported on the qwen_audio dialect.

  • Field Details

    • DEFAULT_END_SESSION_TIMEOUT

      public static final Duration DEFAULT_END_SESSION_TIMEOUT
      Default wait bound for session.finished after session.finish (2.x parity).
  • Method Details

    • connect

      public void connect()
      (Re)connects the session. Idempotent while a socket is open (2.x omni parity); after close() it opens a fresh generation and drops everything buffered from the old one. The handshake wait runs outside the session monitor so close() can always take the monitor promptly and cancel a still-connecting socket. A concurrent connect() returns as soon as a connect attempt owns the socket, i.e. while its handshake may still be in flight.
    • updateSession

      public void updateSession(UpdateSessionParams update)
      session.update with the given configuration (only set fields are serialized).
    • appendAudio

      public void appendAudio(byte[] audio)
      Appends PCM audio; base64-encoded on the wire (protocol limitation).
    • appendAudioBase64

      public void appendAudioBase64(String audioBase64)
      Appends audio already base64-encoded.
    • appendImage

      public void appendImage(byte[] image)
      Appends one image frame of the video input; base64-encoded on the wire.
    • commit

      public void commit()
      Commits the audio buffered so far (not needed in server-VAD mode).
    • clearAppendedAudio

      public void clearAppendedAudio()
      Clears the audio buffered so far.
    • createItem

      public void createItem(com.fasterxml.jackson.databind.JsonNode item)
      Creates a conversation item.
    • createResponse

      public void createResponse()
      response.create with no overrides.
    • createResponse

      public void createResponse(String instructions, List<String> modalities)
      response.create with overrides; instructions is always carried (JSON null when not given, 2.x parity), modalities only when non-null.
    • cancelResponse

      public void cancelResponse()
      Cancels the in-flight response.
    • endSessionAsync

      public void endSessionAsync()
      Sends session.finish without waiting; see endSession(Duration).
    • endSession

      public void endSession()
      session.finish, then waits up to DEFAULT_END_SESSION_TIMEOUT for session.finished.
    • endSession

      public void endSession(Duration timeout)
      session.finish, then blocks until session.finished or timeout; events pumped while waiting stay buffered for events(). Does not close the socket — call close().
    • events

      public Iterable<com.fasterxml.jackson.databind.JsonNode> events()
      Raw event frames, delivered once and released after delivery (a consuming, single-use stream: a second iterator() throws SdkException; the claim resets on connect()); ends when the socket closes (after a completion was seen or a local close) — an early disconnect raises ApiConnectionException mid-iteration, a transport failure raises its mapped exception. Server error-type frames are delivered as ordinary events.
    • sendEvent

      public void sendEvent(com.fasterxml.jackson.databind.node.ObjectNode frame)
      Low-level escape hatch: sends the frame verbatim, auto-adding event_id if absent.
    • getSessionId

      public String getSessionId()
    • getLastResponseId

      public String getLastResponseId()
    • getFirstAudioDelay

      public Duration getFirstAudioDelay()
      response.created to first response.audio.delta; null until both happened.
    • getFirstTextDelay

      public Duration getFirstTextDelay()
      response.created to first response.audio_transcript.delta; null until both happened.
    • close

      public void close()
      Idempotent; the session may be re-connect()ed afterwards. The handshake wait in connect() runs outside this monitor, so this call always takes it promptly; a socket whose handshake is still in flight is cancelled, failing that connect fast instead of blocking here for the whole connect timeout (2.x c891540 parity: okhttp's close alone does not abort an in-flight handshake).
      Specified by:
      close in interface AutoCloseable