Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow

StackOverflow Logo StackOverflow Logo

StackOverflow Navigation

Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Home
  • Add group
  • Feed
  • User Profile
  • Communities
  • Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
Home/ Questions/Q 543
Next

StackOverflow Latest Questions

Saralyn
  • 0
  • 0
SaralynBegginer
Asked: February 18, 20252025-02-18T05:51:45+00:00 2025-02-18T05:51:45+00:00In: PHP

php – Track the connection status from the backend?

  • 0
  • 0
php – Track the connection status from the backend?

I am Working on a Chat Application developed By laravel (Laravel Reverb) and reactjs.

Two Projects are developed separately and also i am configuring with Laravel Passport.

The Backend basically serve the Api and Chat server where the frontend configuring the chat server and implementing Apis.

For Tracking User Activity (Online or Offline Status) I am Configuring Presence Channel.

Previously, my task was, when a user connected to the channel A User History Saved on Redis. Which Perfectly Work without any issue.

Now I am trying to configure user disconnected system. When a user is disconnected from the channel it will remove that user history from Redis.

Now Every where i am founding calling a api from frontend so that it can remove that entity.

But I need to Determine the user disconnection from Backend cause When connection stablish i can see Connection stablish message on debug panel and also connection closed message inside debug console.

So I think i can track the connection and disconnection from the server side rather than implementing something on frontend.

So is there a way to track the connection status from the backend side? I can’t Handel The Laravel Reverb Disconnect When Tab is closed

I am giving you the whole code. So that any one can understand what is doing and what to do next.

user_id;
        $status = $event->status;

        Log::info("User Status Changed: User ID {$user_id} is now {$status}");

        if ($status === "connected") {
            // Store user as online in Redis
            Redis::setex("user:online:{$user_id}", 300, json_encode([
                'user_id' => $user_id,
                'status' => 'online',
                'last_seen' => now()->toDateTimeString(),
            ]));
        } elseif ($status === "disconnected") {
            // Remove user from Redis (user disconnected)
            Redis::del("user:online:{$user_id}");
        }
    }
}

This is my UserStatusListner Where I am determining the User Connected status and store this inside Redis.

user_id = $user_id;
        $this->status = $status;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return new PresenceChannel('presence-user-status-'.$this->user_id);
    }

    public function broadcastAs()
    {
        return 'user.status'; // Custom event name
    }
}

This is my UserStatus Event Where i called when a Presence Channel is successfully connected.

Broadcast::channel('presence-user-status-{user_id}', function ($user, $user_id) {
    // Check the authenticated user from multiple guards
    $staffUser = Auth::guard('staff_users')->user();
    $businessUser = Auth::guard('business_users')->user();
    $normalUser = Auth::guard('users')->user();

    $userData = [];
    if ($normalUser && (int) $normalUser->user_id === (int) $user_id) {
        $userData = [
            'user_id' => $normalUser->user_id,
            'type' => "user"
        ];

    }

    if ($businessUser && (int) $businessUser->user_id === (int) $user_id) {
        $userData = [
            'user_id' => $businessUser->user_id,
            'type' => "business"
        ];
    }

    if ($staffUser && (int) $staffUser->staff_id === (int) $user_id) {
        $userData = [
            'user_id' => $staffUser->staff_id,
            'type' => "staff"
        ];
        
    }

    if (!empty($userData)) {
        Redis::setex("user:online:{$user_id}", 300, json_encode([
            'user_id' => $userData['user_id'],
            'type' => $userData['type'],
            'last_seen' => now()->toDateTimeString(),
        ]));
        
        // Fire the UserStatus event for tracking
        event(new UserStatus($userData['user_id'], 'connected'));
        return $userData;
    }

    return false; 
});

This is my channel Code of the Presence Channel inside routes/channels

Here When user is online i can store the data into Redis but when a user is disconnect i can’t track that. Anyone have any advise or any way to resolve this issue?

N.B – I am Using Laravel 10 Version.

0
  • 0 0 Answers
  • 69 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • Popular
  • Answers
  • W3spoint99

    What is Physics? Definition, History, Importance, Scope (Class 11)

    • 1 Answer
  • W3spoint99

    The Living World – Introduction, Classification, Characteristics, FAQs (Class 11 ...

    • 1 Answer
  • W3spoint99

    Explain - Biological Classification (Class 11 - Biology)

    • 1 Answer
  • Saralyn
    Saralyn added an answer When Humans look at their childhood pictures, the first thing… January 17, 2025 at 3:25 pm
  • Saralyn
    Saralyn added an answer Previously, length was measured using units such as the length… January 17, 2025 at 3:25 pm
  • Saralyn
    Saralyn added an answer Measurement forms the fundamental principle to various other branches of… January 17, 2025 at 3:25 pm

Related Questions

  • Reading fancy apostrophe PHP [duplicate]

    • 0 Answers
  • Unable to send mail via PHPMailer [SMTP->Error: Password not accepted ...

    • 0 Answers
  • Concerns when migrating from PHP 5.6 to 8.4 [closed]

    • 0 Answers
  • Laravel Auth::attempt() error: "Unknown column 'password'" when using a custom ...

    • 0 Answers
  • Core PHP cURL - header origin pass null value

    • 0 Answers

Trending Tags

biology class 11 forces how physics relates to other sciences interdisciplinary science learn mathematics math sets tutorial null sets physics physics and astronomy physics and biology physics and chemistry physics applications science science connections science education sets in mathematics set theory basics types of sets types of sets explained

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

  • About US
  • Privacy Policy
  • Questions
  • Recent Questions
  • Web Stories

© 2025 WikiQuora.Com. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.