Comments (6)
Hello, thanks for reaching out.
Now, after persisting the data in the database, are you dispatching/broadcasting an event to Pusher itself? What do you see in Pusher's dashboard? Any activity?
from laravel-5-messenger.
Hello again,
I see how differents users connects to pusher but nothing happens when I send a message between users. Perphaps, there is a problem with the listener? I don't know how to the bugg it.
Thank you for help
from laravel-5-messenger.
Hi. Could you provide some piece of code for your controller?
from laravel-5-messenger.
yes,sure
`<?php
namespace App\Http\Controllers;
use App\User;
use Carbon\Carbon;
use App\Models\Message;
use App\Models\Participant;
use App\Models\Models;
use App\Models\Thread;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;
use Pusher; // Pusher\Laravel\Facades\Pusher
use Illuminate\Http\Request;
class MessagesController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
/**
* Show all of the message threads to the user.
*
* @return mixed
*/
public function index()
{
// All threads, ignore deleted/archived participants
$threads = Thread::getAllLatest()->get();
// All threads that user is participating in
// $threads = Thread::forUser(Auth::id())->latest('updated_at')->get();
// All threads that user is participating in, with new messages
// $threads = Thread::forUserWithNewMessages(Auth::id())->latest('updated_at')->get();
return view('messenger.index', compact('threads'));
}
/**
* Shows a message thread.
*
* @param $id
* @return mixed
*/
public function show($id)
{
try {
$thread = Thread::findOrFail($id);
} catch (ModelNotFoundException $e) {
Session::flash('error_message', 'The thread with ID: ' . $id . ' was not found.');
return redirect()->route('messages');
}
// show current user in list if not a current participant
// $users = User::whereNotIn('id', $thread->participantsUserIds())->get();
// don't show the current user in list
$userId = Auth::id();
$users = User::whereNotIn('id', $thread->participantsUserIds($userId))->get();
$thread->markAsRead($userId);
return view('messenger.show', compact('thread', 'users'));
}
/**
* Creates a new message thread.
*
* @return mixed
*/
public function create()
{
$users = User::where('id', '!=', Auth::id())->get();
return view('messenger.create', compact('users'));
}
/**
* Stores a new message thread.
*
* @return mixed
*/
public function store()
{
$input = Input::all();
$thread = Thread::create([
'subject' => $input['subject'],
]);
// Message
$message = Message::create([
'thread_id' => $thread->id,
'user_id' => Auth::id(),
'body' => $input['message'],
]);
// Sender
Participant::create([
'thread_id' => $thread->id,
'user_id' => Auth::id(),
'last_read' => new Carbon,
]);
// Recipients
if (Input::has('recipients')) {
// add code logic here to check if a thread has max participants set
// utilize either $thread->getMaxParticipants() or $thread->hasMaxParticipants()
$thread->addParticipant($input['recipients']);
}
// check if pusher is allowed
if(config('chatmessenger.use_pusher')) {
$this->oooPushIt($message);
}
if(request()->ajax()) {
return response()->json([
'status' => 'OK',
'message' => $message,
'thread' => $thread,
]);
}
return redirect()->route('messages');
}
/**
* Adds a new message to a current thread.
*
* @param $id
* @return mixed
*/
public function update($id)
{
try {
$thread = Thread::findOrFail($id);
} catch (ModelNotFoundException $e) {
Session::flash('error_message', 'The thread with ID: ' . $id . ' was not found.');
return redirect()->route('messages');
}
$thread->activateAllParticipants();
// Message
$message = Message::create([
'thread_id' => $thread->id,
'user_id' => Auth::id(),
'body' => Input::get('message'),
]);
// Add replier as a participant
$participant = Participant::firstOrCreate([
'thread_id' => $thread->id,
'user_id' => Auth::id(),
]);
$participant->last_read = new Carbon;
$participant->save();
// Recipients
if (Input::has('recipients')) {
// add code logic here to check if a thread has max participants set
// utilize either $thread->getMaxParticipants() or $thread->hasMaxParticipants()
$thread->addParticipant(Input::get('recipients'));
}
$html = view('messenger.partials.html-message', compact('message'))->render();
// check if pusher is allowed
if(config('chatmessenger.use_pusher')) {
$this->oooPushIt($message);
}
if(request()->ajax()) {
return response()->json([
'status' => 'OK',
'message' => $message,
'html' => $html,
'thread_subject' => $message->thread->subject,
]);
}
return redirect()->route('messages.show', $id);
}
/**
* Send the new message to Pusher in order to notify users.
*
* @param Message $message
*/
protected function oooPushIt(Message $message, $html = '')
{
$thread = $message->thread;
$sender = $message->user;
$data = [
'thread_id' => $thread->id,
'div_id' => 'thread_' . $thread->id,
'sender_name' => $sender->name,
'thread_url' => route('messages.show', ['id' => $thread->id]),
'thread_subject' => $thread->subject,
'message' => $message->body,
'html' => $html,
'text' => str_limit($message->body, 50),
];
$recipients = $thread->participantsUserIds();
if (count($recipients) > 0) {
foreach ($recipients as $recipient) {
if ($recipient == $sender->id) {
continue;
}
$pusher_resp = Pusher::trigger(['for_user_' . $recipient], 'new_message', $data);
// We're done here - how easy was that, it just works!
}
}
return $pusher_resp;
}
/**
* Mark a specific thread as read, for ajax use.
*
* @param $id
*/
public function read($id)
{
$thread = Thread::find($id);
if (!$thread) {
abort(404);
}
$thread->markAsRead(Auth::id());
}
/**
* Get the number of unread threads, for ajax use.
*
* @return array
*/
public function unread()
{
$count = Auth::user()->unreadMessagesCount();
return ['msg_count' => $count];
}
}
I was able to connect different users to the channels created in the pusher dasboard but when I submit the message I don't receive any live alert or nothing. (Data is sent and stored in the DB)
I see the following message in the console.log when I connect one user to the channel. Could it be the reason?
Pusher.ScriptReceivers[1](null, { host: 'timeline33-clientstats1.pusher.com' });
from laravel-5-messenger.
Hi there!
Finally, I found the problem. There was a mismatch between the server time and pusher. Which is already solved and now the app works as intented.
Thank you for you help!
from laravel-5-messenger.
... Just a follow-up, package now support laravel 6. The docs/examples hav been updated, and Pusher is now used slightly different than before. It is more alligned to how laravel likes it.
Cheers!
from laravel-5-messenger.
Related Issues (20)
- Had to edit models HOT 2
- Starred property HOT 2
- I can use two user's table? HOT 1
- Wow! How are you using this package so far? HOT 1
- Vue js ^3.0 + tailwind css >=2.0 examples; Garner 10+ votes or Donos to kickstart Laravel Inertia/Nuxt/Vue/React Native demo development HOT 5
- Integrate Pusher on Laravel 5.6 HOT 1
- Extend Messages Model HOT 1
- extend message model to everride it's fillable HOT 1
- send message individual HOT 1
- how to create group chat using this package HOT 1
- Group by date
- pusher integration problem HOT 9
- Demo link is broken
- No realtime updates conversation? HOT 3
- removeParticipant() HOT 11
- user column name HOT 2
- Laravel 5.8 supprt? HOT 3
- Thread::participantsString() fail with custom User table HOT 5
- One Thread by user (Like Facebook Messenger system) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-5-messenger.