Websockets facilitate real-time, bidirectional communication between web clients and servers, enhancing the responsiveness and interactivity of AG2-powered applications.
IOStream
class to stream both input and output using websockets. The use of
websockets allows you to build web clients that are more responsive than
the one using web methods. The main difference is that the webosockets
allows you to push data while you need to poll the server for new
response using web methods.
In this guide, we explore the capabilities of the
IOStream
class. It is specifically designed to enhance the development of clients
such as web clients which use websockets for streaming both input and
output. The
IOStream
stands out by enabling a more dynamic and interactive user experience
for web applications.
Websockets technology is at the core of this functionality, offering a
significant advancement over traditional web methods by allowing data to
be “pushed” to the client in real-time. This is a departure from the
conventional approach where clients must repeatedly “poll” the server to
check for any new responses. By employing the underlining
websockets library, the IOStream
class facilitates a continuous, two-way communication channel between
the server and client. This ensures that updates are received instantly,
without the need for constant polling, thereby making web clients more
efficient and responsive.
The real power of websockets, leveraged through the
IOStream
class, lies in its ability to create highly responsive web clients. This
responsiveness is critical for applications requiring real-time data
updates such as chat applications. By integrating the
IOStream
class into your web application, you not only enhance user experience
through immediate data transmission but also reduce the load on your
server by eliminating unnecessary polling.
In essence, the transition to using websockets through the
IOStream
class marks a significant enhancement in web client development. This
approach not only streamlines the data exchange process between clients
and servers but also opens up new possibilities for creating more
interactive and engaging web applications. By following this guide,
developers can harness the full potential of websockets and the
IOStream
class to push the boundaries of what is possible with web client
responsiveness and interactivity.
Note: If you have been usingFor more information, please refer to the installation guide.autogen
orag2
, all you need to do is upgrade it using:orasautogen
, andag2
are aliases for the same PyPI package.
config_list_from_json
function loads a list of configurations from an environment variable or
a json file.
on_connect
functionon_connect
function is a crucial part of applications that utilize
websockets, acting as an event handler that is called whenever a new
client connection is established. This function is designed to initiate
any necessary setup, communication protocols, or data exchange
procedures specific to the newly connected client. Essentially, it lays
the groundwork for the interactive session that follows, configuring how
the server and the client will communicate and what initial actions are
to be taken once a connection is made. Now, let’s delve into the details
of how to define this function, especially in the context of using the
AG2 framework with websockets.
Upon a client’s connection to the websocket server, the server
automatically initiates a new instance of the
IOWebsockets
class. This instance is crucial for managing the data flow between the
server and the client. The on_connect
function leverages this instance
to set up the communication protocol, define interaction rules, and
initiate any preliminary data exchanges or configurations required for
the client-server interaction to proceed smoothly.
on_connect
function such as the
one in the example above is defined:
llm_config
as adding a function to one of them will
also attempt to add it to another.
llm_config
as the UserProxyAgent does not use LLM.
initiate_chat
method
of your UserProxyAgent
to start the interaction with the
conversable agent, passing the initial message and a cache mechanism
for efficiency.
on_connect
function with a Python client involves
simulating a client-server interaction to ensure the setup, data
exchange, and communication protocols function as intended. Here’s a
brief explanation on how to conduct this test using a Python client:
IOWebsockets.run_server_in_thread method
to start the server in a
separate thread, specifying the on_connect function and the port.
This method returns the URI of the running websocket server.
on_connect
function, by simulating a
realistic message exchange. It ensures that the server can handle
incoming connections, process messages, and communicate responses back
to the client, all critical functionalities for a robust websocket-based
application.
on_connect
function in a web environment using
FastAPI to serve a simple interactive
HTML page. This method allows users to send messages through a web
interface, which are then processed by the server running the AG2
framework via websockets. Here’s a step-by-step explanation:
run_websocket_server
async
context manager starts the websocket server using
IOWebsockets.run_server_in_thread
with the specified on_connect
function and port. This server listens for incoming websocket
connections.
@app.get("/")
) is defined to serve the HTML page to users. When a
user accesses the root URL, the HTML content for the websocket chat
is returned, allowing them to interact with the websocket server.
on_connect
function using
Python’s built-in http.server
module. This setup allows for real-time
interaction within a web browser, enabling developers to test the
websocket functionality in a more user-friendly and practical manner.
Here’s a breakdown of how this code operates and its potential
applications:
SimpleHTTPRequestHandler
is defined to serve the HTML file. This
handler overrides the do_GET method to redirect the root path (/
)
to the chat.html
page, ensuring that visitors to the server’s root
URL are immediately presented with the chat interface.
IOWebsockets.run_server_in_thread
method, with the previously
defined on_connect
function as the callback for new connections.