Listen for messages from the server.

Send data to the server.

Close the WebSocket connection.

1. Connect to a WebSocket server

The web_socket_channel package provides the
tools you need to connect to a WebSocket server.

The package provides a WebSocketChannel
that allows you to both listen for messages
from the server and push messages to the server.

In Flutter, use the following line to
create a WebSocketChannel that connects to a server:

2. Listen for messages from the server

Now that you’ve established a connection,
listen to messages from the server.

After sending a message to the test server,
it sends the same message back.

In this example, use a StreamBuilder
widget to listen for new messages, and a
Text widget to display them.

How this works

The WebSocketChannel provides a
Stream of messages from the server.