Ever wanted to showcase your Discord status, current gaming session, or Spotify tracks on your personal website? Enter Lantern, a powerful service that makes it incredibly easy to broadcast your Discord presence in real-time. In this post, we'll explore how Lantern works and how you can integrate it into your projects.
Lantern is a service that broadcasts your Discord presence through both a RESTful API and WebSocket connections. It's designed to be simple to use while offering robust functionality for developers who want to display Discord status information on their websites or applications.
The simplest way to get started is by using the REST API. You can fetch a user's presence data with a simple GET request to:
lantern.rest/api/v1/users/:your_id
This endpoint returns comprehensive information about the user, including:
For real-time updates, Lantern provides a WebSocket connection at wss://lantern.rest/socket
. The WebSocket implementation follows a simple protocol:
Lantern includes a built-in key-value storage system that you can use for:
The KV storage system is accessible through both the REST API and WebSocket connections, making it versatile for various use cases.
When fetching user data, you can customize the response with various parameters:
Here's a simple example of how you might implement real-time Discord presence on your website:
const ws = new WebSocket('wss://lantern.rest/socket');
ws.onmessage = event => {
const data = JSON.parse(event.data);
if (data.op === 1) { // Hello
// Set up heartbeat
setInterval(() => {
ws.send(JSON.stringify({ op: 4 }));
}, data.d.heartbeat_interval);
// Initialize connection
ws.send(JSON.stringify({
op: 2,
d: { user_id: "YOUR_DISCORD_ID" }
}));
}
if (data.op === 6) { // Presence Update
// Update your UI with the new presence data
}
};
While Lantern is ready to use out of the box, you can also self-host it if you need complete control over the infrastructure. The project is open-source and includes detailed documentation for setting up your own instance.
The combination of real-time WebSocket updates and a straightforward REST API makes it flexible enough for both simple implementations and complex applications. Plus, with the added bonus of key-value storage, you can extend its functionality to suit your specific needs.
Ready to get started? Head over to the Lantern documentation to begin integrating Discord presence into your projects today!