Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How pusher send data to selected clients only

Tags:

pusher

I have used pusher recently in my PHP laravel project and it is working fine. What I know about pusher is that it is a real time layer between our server and client and creates web socket connection to our client browser. I setup pusher in my application using below tutorial:

pusher integration with laravel

What I have created using pusher for my web application:

1.I have created a notification functionality. Where when one user add some data to database say when one user starts following other user a event is triggered and that event sends data to particulr channel say 'notification-channel' and in my js code I have subscribed to this channel. For that I have written below line of codes:

//instantiate a Pusher object with our Credential's key
    var pusher = new Pusher('68fd8888888888ee72c', {
        encrypted: true
    });

    //Subscribe to the channel we specified in our Laravel Event
    var channel = pusher.subscribe('notification-channel');

    //Bind a function to a Event (the full Laravel class)
    channel.bind('App\\Events\\HelloPusherEvent', addMessage);
  1. By using addMessage() function I display some data. Now I have put a check on client side so that only if logged in user is intended to receive this message by writing simple if condition. As I have sent intended user's id in within data from App\Events\HelloPusherEvent so I used this Id to display msg to specific users only.

But I think this is not right approach to use pusher for notifications or any other functionality. Further in my project I want to use pusher for displaying new news feeds on user's newsfeed without page refresh, where obviously few users will see those posts according to whom is posting that news post.

But How I will user pusher in a way that I don't need to implement if conditions on client side to stop displaying data. Here my concern is that if I will keep sending data to all the active clients and put if conditions to filter data that will ultimately degrade my application.

My concerns:

  1. If pusher sends data to multiple clients that is obviously all the active users then will it not cause overhead.

  2. Is there any option to use channels to channelize data to intended users only.

  3. As I am implementing pusher first time I have few doubts regarding its actual working so is there any blog which can help me to understand its real time working.

Let me know if my question is not clear and specific enough, I will elaborate it further.

Thanks in advance to all who will try to answer.

like image 310
Always_a_learner Avatar asked Jan 05 '23 20:01

Always_a_learner


2 Answers

This Question pusher-app-client-events explained that we can create different channels for different users to send msg to only intended users.

I go through this FAQ and came to know that we can create unlimited channels for one registered APP.

Creating multiple channels won't cause any overhead.

Now if I want to send notification to user 1 then I would create a channel 'notificaton-channel-1' and would subscribe user 1 to same channel within my frontend code.

The event class that I am using within my PHP laravel project looks like below:

<?php

namespace App\Events;

use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
/**
 * Just implement the ShouldBroadcast interface and Laravel will automatically
 * send it to Pusher once we fire it
 **/
class HelloPusherEvent extends Event implements ShouldBroadcast
{
    use SerializesModels;

    /**
     * Only (!) Public members will be serialized to JSON and sent to Pusher
     **/
    public $message;
    public $id;
    public $for_user_id;

    /**
     * Create a new event instance.
     * @param string $message (notification description)
     * @param integer $id (notification id)
     * @param integer $for_user_id (receiver's id)
     * @author hkaur5
     * @return void
     */
    public function __construct($message,$id, $for_user_id)
    {
        $this->message = $message;
        $this->id = $id;
        $this->for_user_id = $for_user_id;
    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        //We have created names of channel on basis of user's id who
        //will receive data from this class.
        //See frontend pusher code to see how we have used this channel
        //for intended user.
        return ['notification-channel_'.$this->for_user_id];
    }
}

and on Frontend I subscribed to 'notification-channel-'+logged_in_user_id

 //Subscribe to the channel we specified in our Laravel Event
    //Subscribe user to the channel created for this user.
    //For example if user's id is 1 then bind to notification-channel_1
    var channel = pusher.subscribe('notification-channel_'+$('#logged_in_userId').val());

    //Bind a function to a Event (the full Laravel class)
    channel.bind('App\\Events\\HelloPusherEvent', addMessage);

This way we can send data to intended users only rather blocking data received by all the users by putting conditions in our client side code.

like image 135
Always_a_learner Avatar answered Jan 07 '23 11:01

Always_a_learner


I think you should add User ID within Blade template directly, not using a form field:

var channel = pusher.subscribe('notification-channel_{{ Auth::id() }}');
like image 40
Tahir Hoxha Avatar answered Jan 07 '23 10:01

Tahir Hoxha