Skip to main content
Tokio provides a comprehensive set of examples showcasing various capabilities of the async runtime. These examples range from simple “hello world” programs to more complex real-world applications.

Getting Started

All examples can be executed from the Tokio repository with:
cargo run --example $name
For beginners, we recommend starting with the hello_world and echo-tcp examples to understand the basics of async networking with Tokio.

Available Examples

Hello World

A simple TCP client that connects and writes data

Echo TCP Server

A concurrent TCP echo server demonstrating basic async I/O patterns

Chat Server

A multi-client chat server with message broadcasting

TCP Client

Connect stdin/stdout to a TCP stream for interactive communication

Proxy Server

Forward data between clients and backend servers

Custom Executor

Integrate Tokio runtime with custom executors

Example Categories

Networking Basics

A minimal TCP client that opens a connection, writes “hello world”, and closes.Use case: Understanding basic async TCP connectionsKey concepts:
  • TcpStream::connect()
  • AsyncWriteExt trait
  • #[tokio::main] macro
cargo run --example hello_world
An interactive client that hooks up stdin/stdout to a TCP stream, allowing you to type messages and receive responses.Use case: Testing TCP servers interactivelyKey concepts:
  • Framed I/O with codecs
  • Stream and Sink traits
  • Bidirectional communication
cargo run --example connect-tcp 127.0.0.1:8080
A TCP proxy that forwards data between clients and a backend server using copy_bidirectional.Use case: Load balancing, protocol translation, debuggingKey concepts:
  • copy_bidirectional()
  • Concurrent connection handling
  • Forwarding patterns
cargo run --example proxy

Server Examples

These examples demonstrate building production-ready servers with Tokio:
  • Echo TCP Server - Learn concurrent connection handling and basic I/O patterns
  • Chat Server - Explore message broadcasting, shared state, and the select! macro
  • UDP Examples - UDP echo server and client implementations

Advanced Topics

Custom Executor

Learn how to integrate Tokio with other executor frameworks using TokioContext.Topics covered:
  • Custom runtime integration
  • Context management
  • Thread pool coordination

Real-World Example

For a larger, production-quality example, check out mini-redis, a Redis implementation built with Tokio. It demonstrates:
  • Command parsing and execution
  • Connection pooling
  • Error handling patterns
  • Testing async code
  • Production-ready architecture
The Tokio tutorial provides guided walkthroughs of several examples with detailed explanations.

Contributing Examples

Have an example you’d like to see? Open an issue in the Tokio repository. If you’ve built something useful, pull requests are welcome!

Next Steps

Start with Echo Server

Build your first concurrent TCP server

Build a Chat App

Learn message broadcasting and state management

Build docs developers (and LLMs) love