0

I am trying to create a one to one response between a user and an admin using laravel and pusher. The user is not logged in, just a visitor. When the user visits the site, they will type something and send it to an admin. If any admin is online they will click yes or no and this will then get sent back to the USER who sent it.

My issue is that I can not seem to get pusher to send the responses. I have seen this Pusher one to one chat structure but i do not quiet understand as in my scenario it is not a chat room and that is more specific to a chat room.

Anyways I have tried to use the session id but that didn't work as the user and admin are not on the same browser. I thought about somehow sending the session id with the pusher notification message but I similar did know how because the admin is on a /admin/events page while the user is on the home page.

This is my code

push.js

  var pusher = new Pusher('MY KEY');

  var channel = pusher.subscribe ('client');


  channel.bind('general_response', addMessage);


  var channel = pusher.subscribe ('customer_response');
  //bind to new_response on the channel
  channel.bind('message', addWaitingResponse);

  //user 
  function addMessage (data) {
    //create list item
    var li = $('<div class="response_message"></div>');
    //Get text properity from data
    li.text(data.text);
    //hide it
    li.hide();
    //prepend to messages
    $('#messages').prepend(li);
    li.slideDown();
  }

//admin
function addWaitingResponse(data) {
    var li = $('<div class="waiting_approve"> </div> ');
    li.text(data.text);
    li.hide();
    $('#WaitingNotification').prepend(li);
    li.slideDown();
  }

subscribing and listening

<?php
 namespace Scan\Subscriber;

class NotificationEventSubscriber {

    private $_pusher;
    public function __construct() {

        $this->_pusher = new \Pusher($_ENV['app_key'], $_ENV['app_secret'], $_ENV['app_id']);

    }

    //message to admin
    public function onCreate($event) {

            //$pusher = new \Pusher($_ENV['app_key'], $_ENV['app_secret'], $_ENV['app_id']);

            $data = array('text' => $event->track_no 'is awaiting approval');

            $this->_pusher->trigger('customer_response', 'message', $data);

        }
        //message to user

    public function onUpdate($event) {
            /*
            | Notify customer
            */

            $message = array('text' => $event->track_no' has been approved ');

            $this->_pusher->trigger('client', 'general_response', $message);


    }

    public function subscribe($events) {

            $events->listen('notification.insert','Scan\Subscriber\NotificationEventSubscriber@onCreate');

            $events->listen('notification.update','Scan\Subscriber\NotificationEventSubscriber@onUpdate');

    }

}

In laravel when the user sends the requests, I register and fire the event. The code above just shows how I am displaying the message to the user and admin

My question is: How do I make the admin response ONLY sent to the user who sent the request? Thank you very much.

Community
  • 1
  • 1
Baako
  • 263
  • 2
  • 4
  • 11

1 Answers1

1

It sounds like you are on the right lines. You need some way of creating a unique identifier that both the anonymous user and the admin user know about. That identifier can then be used to route messages - probably via a Pusher channel.

Since the user is anonymous using the session ID as part of a unique channel name for that user seems like a good idea.

Alternatively you could make the server respond with a unique ID when the anonymous user sends a request for help. With this in mind, here's a diagram showing a potential way of achieving this:

Once the above process has completed the Admin and the anonymous User can now communicate by triggering events on the channel identified by the request_id.

Community
  • 1
  • 1
leggetter
  • 15,248
  • 1
  • 55
  • 61
  • 1
    Thanks @leggetter what I did was store the client channel in the database and the just make sure that the response goes to that channel. Thanks again – Baako Jan 29 '15 at 18:46
  • @leggetter, following your scenario, there is a way for the user to know if admin is 'available' (listening for events) ? – cristian Feb 19 '21 at 11:33
  • 1
    @cristian Pusher does support Presence channels so you could potentially use those https://pusher.com/docs/channels/using_channels/presence-channels If the `admin` user is one of the members then they are online. – leggetter Feb 22 '21 at 13:10
  • @leggetter will I be able to update the admin online/offline state in real time for the other clients/users to see ? how can I acchive that ? – cristian Feb 22 '21 at 16:59
  • Presence channels provide realtime updates regarding whether a member is present on a channel or not (online/offline). So, yes. More info on this can be found in the docs. – leggetter Feb 25 '21 at 15:36