Detect user is Online or Offline using Livewire in Laravel

If you are using Livewire then you don’t need to include any external library or write a script to detect if a user is online or offline.

Livewire already provided a utility to perform offline action check. You only need to add it to the HTML element that gets affected according to the user state.

In this tutorial, I show how you can detect if the user is online or offline and display an alert using Livewire in Laravel 9.

Detect user is online or offline using Livewire in Laravel


  1. Create Controller
  2. Create Route
  3. Create Livewire Component
  4. Create View
  5. Output
  6. Conclusion

1. Create Controller

  • Create ContactController controller.
php artisan make:controller ContactController
  • Open app\Http\Controllers\ContactController.php file.
  • Create 1 method –
    • index() – Load index view.

Completed Code


namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ContactController extends Controller
    public function index(){
        return view('index');

2. Create Route

  • Open routes/web.php file.
  • Define 1 route –
    • / – Load index view.

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\ContactController;

Route::get('/', [ContactController::class, 'index']);

3. Create Livewire Component

  • Create contact-component component –
php artisan make:livewire contact-component

This will create 2 files –

  • app/Http/Livewire/ContactComponent.php
  • resources/views/livewire/contact-component.blade.php


Create 4 class variables –

  • $name, $email, and $message use for data binding.
  • $success use for form status.

Methods –

  • submit() – It calls on <form > submit. Validate the values and assign 1 to $success.
  • render() – Load component view.

Completed Code


namespace App\Http\Livewire;

use Livewire\Component;

class ContactComponent extends Component

     public $name = "";
     public $email = "";
     public $message = "";
     public $success = 0;

     public function submit(){

          // Validate
               'name' => 'required',
               'email' => 'required|email',
               'message' => 'required',

          // Success
          $this->success = 1;


     public function render()
          return view('');


  • Add wire:offline to display alert message when the user is offline.
  • Display success message if $success == 1.
  • Create <form > and add wire:submit.prevent="submit".
  • Add input elements and in the submit button add wire:offline.attr="disabled" to disable the button when offline.

Completed Code


     <div class="container mt-5">

          <!-- Offline alert -->
          <div class="alert alert-danger w-100" wire:offline>
               You are offline. Please check your Internet connection.

          <!-- Success Message -->
          @if($success == 1)
          <div class="alert alert-success">
               Submit successfully

          <h2>Contact form</h2>
          <form wire:submit.prevent="submit">

               <div class="form-group">
                    <label for="name">Name:</label>
                    <input type="text" class="form-control" id="name" placeholder="Enter name" wire:model="name" value="">
                         <div class="error text-danger">* {{ $message }}</div> 
               <div class="form-group">
                    <label for="email">Email:</label>
                    <input type="email" class="form-control" id="email" placeholder="Enter email" wire:model="email" value="">
                         <div class="error text-danger">* {{ $message }}</div> 
               <div class="form-group">
                    <label for="message">Message:</label>
                    <textarea class="form-control" id="message" placeholder="Enter message" wire:model="message" ></textarea>
                         <div class="error text-danger">* {{ $message }}</div> 

               <!-- Disabled button when Offline -->
                    class="btn btn-primary mt-2"


4. Create View

Create index.blade.php file in resources/views/ folder.

Add contact-component component and include livewire style and script.

Completed Code

<!DOCTYPE html>
     <meta charset="utf-8">
     <meta name="viewport" content="width=device-width, initial-scale=1">

     <title>Detect user is online or offline in Livewire - Laravel</title>

     <link rel="stylesheet" href="" >


     <livewire:contact-component />



5. Output

View Output

6. Conclusion

With this, you can easily alert the user when disconnected from the internet and stop the user to submit the form or any other actions.

You have to add wire:offline to element.

If you found this tutorial helpful then don't forget to share.

Leave a Comment