You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Echo Server(Socket.io)无Pusher/Redis实现新用户加入通知方案咨询

Hey there! I’ve been in a similar spot working with Laravel Echo Server + Socket.io without Pusher/Redis, so I can help you out with this. Here’s how you can implement the "new user joined" notification functionality:

方案1:自定义事件 + 手动广播(最直接且易维护)

Since Laravel Echo Server doesn’t have a built-in OnJoin hook, you can manually trigger a custom event when a user successfully connects and subscribes to a channel, then broadcast that event to all channel members.

Step 1: Create a custom broadcast event in Laravel

First, generate an event class that implements the ShouldBroadcast interface:

php artisan make:event UserJoinedChannel

Then edit the event file to define the broadcast channel and user data:

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use App\Models\User;

class UserJoinedChannel implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    // Define the channel to broadcast on (use public/private as needed)
    public function broadcastOn(): array
    {
        return [
            new Channel('chat-room'), // Replace with your target channel name
        ];
    }

    // Optional: Customize the event name for frontend listening
    public function broadcastAs()
    {
        return 'user.joined';
    }
}

Step 2: Trigger the event when a user subscribes to the channel

In your Vue.js frontend, after successfully subscribing to the channel, send an API request to your Laravel backend to trigger the event:

// Initialize Echo first (make sure this is set up correctly)
Echo.channel('chat-room')
    .listen('user.joined', (e) => {
        // Update your UI with the notification
        alert(`${e.user.name} has joined the channel!`);
    })
    .subscribe(() => {
        // Send request to backend after successful subscription
        axios.post('/api/user-joined', {
            channel: 'chat-room'
        });
    });

Then set up the corresponding route and controller method in Laravel:

// routes/api.php
Route::post('/api/user-joined', [UserController::class, 'notifyJoined']);

// app/Http/Controllers/UserController.php
public function notifyJoined(Request $request)
{
    // Ensure the user is authenticated
    if (!auth()->check()) {
        return response()->json(['error' => 'Unauthorized'], 401);
    }

    // Dispatch the broadcast event
    event(new \App\Events\UserJoinedChannel(auth()->user()));
    return response()->json(['status' => 'Notification sent']);
}

Step 3: Verify Echo Server configuration

Double-check your laravel-echo-server.json to ensure the authEndpoint points to your Laravel app’s /broadcasting/auth route, and channel permissions are set correctly (public channels don’t require extra auth, private channels need proper authentication logic).

方案2: Modify Laravel Echo Server source code (advanced)

If you prefer not to use an API trigger, you can modify the Echo Server’s source code to add a custom onJoin hook. Note that this requires maintaining your modified code through Echo Server updates, so use it cautiously.

  1. Locate the core channel logic files (usually in src/channels/Channel.js or src/Socket.io/SocketIoAdapter.js in the Echo Server package).
  2. Add code to the channel’s join method to broadcast a "user joined" message directly:
// Example modification in Channel.js
join(socket, data) {
    // Existing join logic...
    
    // Broadcast to all channel members
    this.socket.emit('user.joined', {
        user: data.user, // Ensure user data is passed during authentication
        channel: this.name
    });
}

You can also call your Laravel broadcast API directly from this hook if you need to process additional server-side logic.

Key Notes
  • Since you’re not using Redis/Pusher, Laravel’s built-in presence channel functionality won’t work (it requires centralized storage to track online users). Stick to public or private channels with manual event triggers.
  • Always validate user authentication to prevent unauthorized users from triggering notifications.
  • Test the subscription success timing in your frontend to ensure the API request is sent only after the user is fully connected to the channel.

内容的提问来源于stack exchange,提问作者Pankaj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:13:59