Simple event-based client-server networking library for Bevy

Overview

Bevy Client Server Events

Bevy Client Server Events Latest version Documentation MIT Apache

Simple event-based client-server networking library for Bevy.

Easily send bevy events to/from a client/server without worrying about serialization or network transport details.

Builds off of the renet/bevy_renet library and attempts to simplify the configuration and management of types to be sent through a network.

Goals:

  • Simplified network setup and configuration
  • Easily send any types to/from a client/server

Requirements:

  • serde to derive Serialize and Deserialize on your types

Events

The following events are useful for servers:

  • EventWriter<StartServer> - Send this event to start a server
  • EventWriter<StopServer> - Send this event to stop a running server
  • EventReader<ClientConnected> - Received whenever a new client is connected
  • EventReader<ClientDisconnected> - Received whenever a client has disconnected
  • EventReader<ReceiveFromClient<T>> - Received whenever a client has sent type T to the server
  • EventWriter<SendToClient<T>> - Send this event to have a particular client receive type T
  • EventWriter<SendToClients<T>> - Send this event to have all connected clients receive type T

The following events are useful for clients:

  • EventWriter<ConnectToServer> - Send this event to connect to a server
  • EventWriter<DisconnectFromServer> - Send this event to disconnect from the server
  • EventWriter<SendToServer<T>> - Send this event to have the server receive type T
  • EventReader<ReceiveFromServer<T>> - Received whenever the server has sent type T to the client

Both the client and the server can receive the EventReader<NetcodeTransportError> events to deal with networking errors.

Examples

There are a few examples in the examples/ directory.

Ping

See the examples/ping.rs file for a simple ping-pong example.

In one terminal session, start the server: cargo run --example ping -- -s

In another terminal session, connect with a client: cargo run --example ping

With the client window in focus, hit ENTER to send a Ping. The server will respond with a Pong.

In this example, we want to send a Ping event to the server and receive a Pong event in return.

#[derive(Event, Serialize, Deserialize)]
pub struct Ping;

#[derive(Event, Serialize, Deserialize)]
pub struct Pong;

When setting up our App, we need to pass it to the client_server_events_plugin macro and provide all the types to be sent over the network.

fn main() {
    // ...
    let mut app = App::new();
    client_server_events_plugin!(
        app,
        Ping => NetworkConfig::default(),
        Pong => NetworkConfig::default()
    );
    // ...

You can provide type-specific network configuration, such as reliability, resend time, max memory usage, etc.

The macro should be run regardless of whether this instance will be a server or a client.

You can choose to start a server instance or connect to a server as a client using events.

fn start_server(mut start_server: EventWriter<StartServer>) {
    start_server.send(StartServer::default()); // Binds to 127.0.0.1:5000 with no encryption by default.
}

fn connect_as_client(mut connect_to_server: EventWriter<ConnectToServer>) {
    connect_to_server.send(ConnectToServer::default()); // Connects to 127.0.0.1:5000 with no encryption by default.
}

Then you can send/receive events as desired.

fn update_client(
    mut send_ping: EventWriter<SendToServer<Ping>>,
    mut receive_pong: EventReader<ReceiveFromServer<Pong>>,
) {
    // ...
    send_ping.send(SendToServer { content: Ping });
    // ...
    for ReceiveFromServer { content } in receive_pong.iter() {
        // Do something with content (Pong).
        // ...
    }
}

fn update_server(
    mut receive_ping: EventReader<ReceiveFromClient<Ping>>,
    mut send_pong: EventWriter<SendToClient<Pong>>,
) {
    for ReceiveFromClient { client_id, content } in receive_ping.iter() {
        // Do something with content (Ping).
        send_pong.send(SendToClient {
            client_id,
            content: Pong,
        });
    }
}

Features Example

See the examples/features.rs file for examples of more features, such as encryption, broadcasting, networking error handling, and client connect/disconnect events.

In one terminal session, start the server: cargo run --example features -- -s

In another terminal session, connect with a client: cargo run --example features

The server and client will use encryption to communicate.

Every 500 frames the server will broadcast a message of it's frame count.

With focus on the server window:

  • Hit ESC to stop the server
  • Hit ENTER to start the server

With focus on the client window:

  • Hit ESC to disconnect from the server
  • Hit ENTER to reconnect to the server
  • Hit SPACE to send a message of type PlayerMovement

The server will respond to the PlayerMovement message with a ServerResponse message.

Other Networking Crates

This crate was created because I wanted the quickest and easiest way to send types through a network.

Other solutions seem to have me bogged down in transport/networking/channel details.

This crate is ideal for prototyping and small client-server games.

An alternative with more bells & whistles would be bevy_replicon.

A mature alternative with more customizability would be bevy_renet.

Bevy Compatibility

bevy bevy_client_server_events
0.11 0.5
You might also like...
bevy-hikari is an implementation of voxel cone tracing global illumination with anisotropic mip-mapping in Bevy
bevy-hikari is an implementation of voxel cone tracing global illumination with anisotropic mip-mapping in Bevy

Bevy Voxel Cone Tracing bevy-hikari is an implementation of voxel cone tracing global illumination with anisotropic mip-mapping in Bevy. Bevy Version

Minecraft using Bevy and Bevy-Meshem

minecraft_bevy Minecraft_bevy was built to showcase bevy_meshem. After a week of developing it has: Chunk loading / unloading each chunk's mesh is bei

Brine is my attempt at writing a Minecraft client in Rust using the Bevy game engine.
Brine is my attempt at writing a Minecraft client in Rust using the Bevy game engine.

Brine Brine is my attempt at writing a Minecraft client in Rust using the Bevy game engine. It's EXTREMELY work-in-progress. The thing that makes Brin

Solana Game Server is a decentralized game server running on Solana, designed for game developers

Solana Game Server* is the first decentralized Game Server (aka web3 game server) designed for game devs. (Think web3 SDK for game developers as a ser

🎮 A Realtime Multiplayer Server/Client Game example built entirely with Rust 🦀

Example of a 🎮 Realtime Multiplayer Web Game Server/Client built entirely using Rust 🦀

A physics lib for the bevy game engine based on physme

physimple Physimple aims to be the simplest(and capable) physics engine(currently for bevy) WARNING Beware for breaking changes with each update for n

Bevy plugin for an AssetServer that can load embedded resources, or use other AssetServers based on the path.

Bevy-Embasset Embed your asset folder inside your binary. bevy-embasset adds support for loading assets embedded into the binary. Furthermore, it can

Rapidly iterate and build Bevy UI's with existing web-based technologies
Rapidly iterate and build Bevy UI's with existing web-based technologies

bevy_webview WIP   Rapidly iterate and build Bevy UI's with existing web-based technologies It is currently very early days of this plugin - only Linu

Action-based animation system for Bevy.

bevy_action_animation Action-based animation system for Bevy. Introduction This plugin provides users of the Bevy game engine with an action/trigger-b

Owner
Edouard Poitras
Edouard Poitras
A Client/Server game networking plugin using QUIC, for the Bevy game engine.

Bevy Quinnet A Client/Server game networking plugin using QUIC, for the Bevy game engine. Bevy Quinnet QUIC as a game networking protocol Features Roa

Gilles Henaux 65 Feb 20, 2023
General purpose client/server networking library written in Rust, built on top of the QUIC protocol which is implemented by quinn

Overview "This library stinks!" ... "Unless you like durian" durian is a client-server networking library built on top of the QUIC protocol which is i

Michael 92 Dec 31, 2022
A cross platform (wasm included) networking library!

bootleg_networking A cross platform (wasm included) networking library! A networking plugin for the Bevy game engine that wraps around bevy_networking

William Batista 51 Jan 1, 2023
A simple extension for `bevy-editor-pls` to support tilemap editing right inside the bevy app.

What is this This is a simple tilemap editor plugin, that hooks right into bevy_editor_pls to work with bevy_ecs_tilemap. It works completely within i

null 3 May 8, 2023
Bevy Simple Portals is a Bevy game engine plugin aimed to create portals.

Portals for Bevy Bevy Simple Portals is a Bevy game engine plugin aimed to create portals. Those portals are (for now) purely visual and can be used t

Sélène Amanita 11 May 28, 2023
A scriptable MIDI event processor.

mep Introduction mep is a scriptable midi event processor. It uses koto scripts to process incoming midi events. I/O Every instance of mep introduces

Ali Somay 6 Apr 21, 2022
2D and 3D physics engine based on Extended Position Based Dynamics for Bevy.

Bevy XPBD Bevy XPBD is a 2D and 3D physics engine based on Extended Position Based Dynamics (XPBD) for the Bevy game engine. Design Below are some of

Joona Aalto 203 Jul 6, 2023
Victorem - easy UDP game server and client framework for creating simple 2D and 3D online game prototype in Rust.

Victorem Easy UDP game server and client framework for creating simple 2D and 3D online game prototype in Rust. Example Cargo.toml [dependencies] vict

Victor Winbringer 27 Jan 7, 2023
A simple camera for properly displaying tile-based low resolution pixel perfect 2D games in bevy.

Bevy Tiled Camera A simple camera for properly displaying low resolution pixel perfect 2D games in bevy. The camera will adjust the viewport to scale

sark 10 Oct 5, 2022
Minecraft-esque voxel engine prototype made with the bevy game engine. Pending bevy 0.6 release to undergo a full rewrite.

vx_bevy A voxel engine prototype made using the Bevy game engine. Goals and features Very basic worldgen Animated chunk loading (ala cube world) Optim

Lucas Arriesse 125 Dec 31, 2022