This script demonstrates how to use the fleare client library to create a connection to a remote Fleare server instance, handle lifecycle events, and cleanly manage the connection lifecycle using an asynchronous pattern.
const client = fleare.createClient("127.0.0.1", 9219, {
poolSize: 2,
username: "root",
password: "root",
connectTimeout: 30000, // Max time to wait before failing a connection attempt (in ms)
maxRetries: 3, // Number of retry attempts if connection fails
retryInterval: 10000 // Delay between retries (in ms)
});
This creates a new client with a connection pool size of 2, authentication credentials, and retry logic.
func monitorEvents(client *fleare.Client) {
for event := range client.Events() {
switch event.State {
case fleare.StateConnecting:
fmt.Println("Event is connecting...")
case fleare.StateConnected:
fmt.Println("Event connected successfully!")
case fleare.StateDisconnecting:
fmt.Println("Event Disconnecting")
case fleare.StateDisconnected:
fmt.Println("Event Disconnected")
case fleare.StateError:
fmt.Printf("Event error: %v", event.Error)
}
}
}