How to Install and Use Rivet for Real-Time Applications đ
Tuesday, Dec 24, 2024 | 8 minute read
đ Elevate Your Real-Time App Game! đ This cutting-edge platform streamlines real-time app development with efficient remote procedure calls, intelligent state management, and automatic scaling, ensuring lightning-fast performance and exceptional user experiences! âĄī¸đģ
1. Introduction to Rivet: Key Features That Disrupt Tradition đ
“In today’s rapidly evolving tech landscape, building efficient real-time applications has become a significant challenge for developers.”
In this information age, the real-time capability of applications is more crucial than ever! đī¸ With users’ expectations for speed and responsiveness continuously rising, developers are tasked with the responsibility of quickly constructing high-performance, stable real-time applications. Rivet shines brightly like a dazzling new star, standing out among numerous development platforms by offering developers incredible solutions. đ
Rivet is a platform dedicated to comprehensively supporting real-time applications, enabling developers to easily build efficient applications! ⨠This platform is packed with robust features, including remote procedure calls (RPC), state management, event handling, and edge networking, greatly streamlining the development process. Rivet adopts the concept of Actors, simplifying distributed computing while enhancing system efficiency and scalability. đĒ
2. Highlights of Rivet
The key features of Rivet are definitely worth noting! Firstly, it boasts efficient remote procedure calls (RPC) that allow developers to easily implement cross-application function calls, reducing coding complexity and workload. đ Secondly, the intelligent state management feature automatically handles and persists application data, alleviating developers’ burdens and making data management more efficient. đ In terms of event management, Rivet supports real-time state changes through an integrated publish/subscribe mechanism, ensuring the system can quickly respond to various changes. âĄī¸ Additionally, the platform’s automatic scaling and concurrent processing features allow it to auto-scale based on different needs, enhancing processing efficiency. đ Finally, edge network technology guarantees globally distributed applications, ensuring low-latency performance and providing users with an exceptional experience. đ
3. Developer’s Choice: Why Rivet is the Go-To Platform đ¤
By choosing Rivet, developers gain the advantage of globally optimized performance, allowing their applications to run closer to users, thus reducing latency and boosting application performance for a smoother user experience. đ Furthermore, Rivet’s system can scale to a zero state, optimizing resource management and ensuring that even handling millions of connections doesn’t incur high costs. đ° Comprehensive monitoring capabilities allow developers to easily track application performance, promptly identify issues, and enhance overall development efficiency. đ Data compliance guarantees add peace of mind, ensuring that data is stored in specific locations to meet regulatory requirements, providing strong compliance support for enterprises. âī¸ Lastly, Rivet leverages V8 and Deno to offer a lightweight, cost-effective solution that replaces traditional reliance on lambda functions and container architectures, driving developers forward! đ
4. Rivet Cloud: A Seamless Cloud Hosting Platform âī¸
Rivet Cloud provides a complete environment for deploying Rivet Actors, eliminating the hassles of infrastructure maintenance. đ§ This platform also comes with team collaboration tools that support multiple developers working together, significantly increasing development efficiency. đ¤ An integrated DDoS mitigation mechanism ensures applications run safely and stably, further bolstering user confidence. đĄī¸ Additionally, Rivet Cloud offers powerful release management options, simplifying the update and version management processes, ensuring quick responses to market demands. đ Lastly, the automatic implementation of SSL/TLS encryption allows users to enjoy secure connectivity and protect data privacy, undoubtedly another major attraction for developers! đ
5. Use Cases and Application Scenarios đšī¸
Rivet is particularly well-suited for developing collaborative applications, locally prioritized applications, AI agents, game servers, and chat applications, among other real-time applications. đŽâ¨ Its flexibility and efficiency empower developers to fully unleash their innovative potential, creating more engaging and interactive products! đ
6. Getting Started with RIVET đ
The initial setup is remarkably straightforward, allowing users to quickly start using Rivet’s Actors. The documentation provides detailed setup guides, making it easy for newcomers to get started. đ Our comprehensive documentation is always available for reference, including links to support channels such as Discord and GitHub, helping developers easily find assistance and share ideas! đŠâđģđ¨âđģ
7. Installing Rivet đ
Before using Rivet, ensure it is correctly installed on your system. Rivet offers a simple command line that works across various operating systems!
For macOS, Linux, and WSL users, you can rapidly install the Rivet CLI with the following command:
curl -fsSL https://releases.rivet.gg/rivet/latest/install.sh | sh
In this command, curl
is used to download content from a specified URL, and the -fsSL
flags prevent displaying progress while following redirects, with sh
executing the subsequent script. This command will download and execute Rivet’s installation script from the official source for an easy installation! đ ī¸
For Windows (cmd) users, executed via PowerShell, the command is:
powershell -Command "iwr https://releases.rivet.gg/rivet/latest/install.ps1 -useb | iex"
Here, iwr
is shorthand for Invoke-WebRequest
, used to download files from URLs, with the -useb
option indicating to use a simple HTTP request. The pipeline |
passes the downloaded content to iex
(Execute-Command) for execution. This way, the installation process occurs automatically without requiring extra manual intervention! đ
If you’re a Windows (PowerShell) user, you can similarly use:
iwr https://releases.rivet.gg/rivet/latest/install.ps1 -useb | iex
This has the same effect as the cmd method mentioned above, just executed in PowerShell. đģ
Want to build Rivet from source code? Follow these steps:
git clone https://github.com/rivet-gg/rivet
cargo build --bin rivet
git clone
will pull the Rivet repository to your local machine, while cargo build
will compile the project, generating an executable. Once compiled, you’ll find the executable file in the _target/debug/rivet_
directory! đĒ
8. Starting a Single Node Cluster đ
Once installed successfully, you can start a single node cluster locally for easy development and testing. Use the following Docker command:
docker run --name rivet -v "$(pwd)/rivet-data:/data" -p 8080:8080 -p 9000:9000 -p 7080:7080 -p 7443:7443 --platform linux/amd64 rivetgg/rivet
Here’s a brief explanation of the command parts:
docker run
is the basic command for running a container.--name rivet
specifies the container’s name asrivet
.-v "$(pwd)/rivet-data:/data"
maps therivet-data
folder in the current directory to the container’s/data
directory, ensuring data persistence. â¨- The
-p
options map ports so external access can be made to the services inside the container, necessary for Rivet’s services. --platform linux/amd64
specifies the architecture for running the container.- Finally,
rivetgg/rivet
is the name of the image used for Rivet.
Want to enable TCP and UDP support? Use this command:
docker run --name rivet -v "$(pwd)/rivet-data:/data" -p 8080:8080 -p 9000:9000 -p 7080:7080 -p 7443:7443 -p 7500-7599:7500-7599 -p 7600-7699:7600-7699 --platform linux/amd64 rivetgg/rivet
This command adds more port mappings to handle complex network requests, greatly enhancing application flexibility and scalability! đĄī¸
If you prefer manage Rivet through Docker Compose, consider the following configuration:
services:
rivet:
image: rivetgg/rivet
platform: linux/amd64
volumes:
- rivet-data:/data
ports:
- "8080:8080"
- "9000:9000"
- "7080:7080"
- "7443:7443"
- "7500-7599:7500-7599"
- "7600-7699:7600-7699"
volumes:
rivet-data:
Here, we’ve created a service named rivet
, mapping ports through ports
to ensure access to Rivet’s services from the local machine. The volumes
section guarantees data persistence. đĻ
9. Developing with Rivet đģ
For developers who want to dive deeper, you can compile Rivet from scratch to initiate a complete cluster. Use this command:
docker compose -f docker/dev-full/docker-compose.yml up -d
By this command, Docker Compose will start all necessary services for Rivet according to the provided docker-compose.yml
configuration. Simply type http://localhost:8080 in your browser to access Rivet’s management interface for real-time monitoring and management! đī¸
10. Example Code đ
Next, we will show you how to create a simple real-time chat room. The code example is divided into two parts: Actor and Client.
Actor Code (actor.ts) đ§âđģ
Let’s start by implementing an Actor called ChatRoom
responsible for handling message sending:
class ChatRoom extends Actor {
// Receives remote procedure calls from clients
sendMessage(rpc: Rpc, username: string, message: string) {
// Save the message to persistent storage
this._state.messages.push({ username, message });
// Broadcast the message to all clients
this._broadcast("newMessage", { username, message });
}
}
In the code above, the sendMessage
method serves as a public interface that allows clients to send messages to the chat room. The rpc
parameter supports remote calls while username
and message
represent the sender’s name and message content, respectively. Here, the message is saved to the Actor’s state (_state
) for later use. The _broadcast
method sends the new message via an event broadcast to all connected clients, allowing everyone to see the latest updates instantly! đ
Client Code (client.ts) đą
Below is how to implement sending and receiving messages from the client-side:
const room = await rivet.get<ChatRoom>({
name: "room",
channel: "random"
});
// Click the button to send messages
button.onclick = () => {
room.sendMessage("joe_armstrong", input.value);
};
// Receive broadcast messages from the Actor
room.on("newMessage", ({ username, message }) => {
console.log("New message:", username, message);
});
In this part of the code, we are obtaining the room
Actor through rivet.get<ChatRoom>()
. When the button is clicked, it sends a message using the sendMessage
method. đŠ Simultaneously, by subscribing to the newMessage
event using the on
method, the client will receive new messages in real-time and log them to the console, ensuring users can see updates from other chatters! đ
Rivet simplifies the interface and offers powerful features that make developing real-time applications easy and efficient! Whether you’re developing a chat application or any other project that requires real-time communication, Rivet will provide stable support and convenient solutions.