问题
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.
回答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
.
来源:https://stackoverflow.com/questions/28144354/one-to-one-response-using-pusher