{type: 'Autherror', ошибка: 'json возвращался из-за автоавторизации канала
endpoint была недействительной, но код состояния был 200. Данные были:',
200} ошибка. 200. Данные были: "Статус
ChatController
public function store(StoreMessageRequest $request, Utils $utils)
{
$data = $request->validated();
// Validate multiple files
$userId = auth("sanctum")->user()->id;
$chatId = $data['chat_id'];
$data["chat_identity"] = $chatId;
$data['user_id'] = $userId;
$data["message_id"] = CommonHelpers::generateCramp("post");
$data["chat_id"] = Chat::where("identity", $data["chat_id"])->first()->id;
$images = $request->file('images');
$chatMessage = ChatMessage::create($data);
if ($request->hasFile('images')) {
foreach ($images as $key => $chatImage) {
$milliseconds = round(microtime(true) * 1000);
$imageName = 'image_' . $milliseconds . '.' . $chatImage->extension();
$chatImages = $utils->UploadFile($imageName, $chatImage, "chat_images");
ChatAttachments::create([
"message_id" => $chatMessage->id,
"disk" => "public",
"path" => $chatImages["filePath"],
"mime" => $chatImage->extension(),
"size" => $chatImage->getSize(),
"meta" => null,
]);
}
}
$chatMessage->load('user');
$chatMessages = new \App\Http\Resources\ChatMessage($chatMessage);
$this->sendNewNotificationToOthers($chatMessages);
return $utils->message("success", "Message has been sent successfully...", 200);
}
newMessageSent
class NewMessageSent implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* NewMessageSent constructor.
*
* @param ChatMessage $chatMessage
*/
private $chatMessage;
public function __construct(\App\Http\Resources\ChatMessage $chatMessage)
{
$this->chatMessage = $chatMessage;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
$identity = Chat::where("id", $this->chatMessage->chat_id)->first()->identity;
return new PrivateChannel('chat.' . $identity);
}
/**
* Broadcast's event name
*
* @return string
*/
public function broadcastAs(): string
{
return 'message.sent';
}
/**
* Data sending back to client
*
* @return array
*/
public function broadcastWith(): array
{
return [
'chat_id' => $this->chatMessage->chat_id,
'message' => new \App\Http\Resources\ChatMessage($this->chatMessage)
];
}
}
bootstrap.js
import Echo from "laravel-echo";
import Pusher from "pusher-js";
window.Pusher = Pusher;
Pusher.logToConsole = true;
window.Echo = new Echo({
broadcaster: "reverb",
key: import.meta.env.VITE_REVERB_APP_KEY,
cluster: import.meta.env.VITE_PUSHER_APP_CLUSTER ?? "mt1",
wsHost: import.meta.env.VITE_REVERB_HOST ?? window.location.hostname,
wsPort: import.meta.env.VITE_REVERB_PORT ?? 80,
wssPort: import.meta.env.VITE_REVERB_PORT ?? 443,
forceTLS: false,
encrypted: false,
authEndpoint: "/api/broadcasting/auth",
enabledTransports: ["ws", "wss"],
auth: {
headers: {
Authorization: `Bearer ${localStorage.getItem("auth_token")}`,
Accept: "application/json",
contentType: "application/json",
},
},
});
/**
* Echo exposes an expressive API for subscribing to channels and listening
* for events that are broadcast by Laravel. Echo and event broadcasting
* allow your team to quickly build robust real-time web applications.
*/
import './echo';
Api.php
Broadcast::routes(['middleware' => ['auth:sanctum']]);
javascript
console.log("starting okay...")
Echo.private(`chat.${chatIdentity}`)
.subscribed(() => {
console.log("
})
.listenToAll((event, name) => {
console.log("event", event)
})
.listen(".message.sent", (e) => {
console.log("
})
.error((error) => {
console.error("
});
Подробнее здесь: https://stackoverflow.com/questions/797 ... ation-is-s
Мобильная версия