Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion resources/views/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ class="rounded-full px-3 py-1 inline-block text-sm"
form: {
channel: null,
event: null,
data: null,
data: {},
},
logs: [],
},
Expand Down Expand Up @@ -396,6 +396,8 @@ class="rounded-full px-3 py-1 inline-block text-sm"
let payload = {
_token: '{{ csrf_token() }}',
appId: this.app.id,
key: this.app.key,
secret: this.app.secret,
channel: this.form.channel,
event: this.form.event,
data: JSON.stringify(this.form.data),
Expand Down
51 changes: 25 additions & 26 deletions src/Dashboard/Http/Controllers/SendMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,51 @@

namespace BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers;

use BeyondCode\LaravelWebSockets\Contracts\ChannelManager;
use BeyondCode\LaravelWebSockets\Concerns\PushesToPusher;
use BeyondCode\LaravelWebSockets\Rules\AppId;
use Exception;
use Illuminate\Http\Request;

class SendMessage
{
use PushesToPusher;

/**
* Send the message to the requested channel.
*
* @param \Illuminate\Http\Request $request
* @param \BeyondCode\LaravelWebSockets\Contracts\ChannelManager $channelManager
* @return \Illuminate\Http\Response
*/
public function __invoke(Request $request, ChannelManager $channelManager)
public function __invoke(Request $request)
{
$request->validate([
'appId' => ['required', new AppId],
'key' => 'required|string',
'secret' => 'required|string',
'channel' => 'required|string',
'event' => 'required|string',
'data' => 'required|json',
]);

$payload = [
'channel' => $request->channel,
'event' => $request->event,
'data' => json_decode($request->data, true),
];

// Here you can use the ->find(), even if the channel
// does not exist on the server. If it does not exist,
// then the message simply will get broadcasted
// across the other servers.
$channel = $channelManager->find(
$request->appId, $request->channel
);

if ($channel) {
$channel->broadcastToEveryoneExcept(
(object) $payload,
null,
$request->appId
);
} else {
$channelManager->broadcastAcrossServers(
$request->appId, $request->channel, (object) $payload
$broadcaster = $this->getPusherBroadcaster([
'key' => $request->key,
'secret' => $request->secret,
'id' => $request->appId,
]);

try {
$decodedData = json_decode($request->data, true);

$broadcaster->broadcast(
[$request->channel],
$request->event,
$decodedData ?: []
);
} catch (Exception $e) {
return response()->json([
'ok' => false,
'exception' => $e->getMessage(),
]);
}

return response()->json([
Expand Down
23 changes: 8 additions & 15 deletions tests/Dashboard/SendMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,28 @@ public function test_can_send_message()
$this->actingAs(factory(User::class)->create())
->json('POST', route('laravel-websockets.event'), [
'appId' => '1234',
'key' => 'TestKey',
'secret' => 'TestSecret',
'channel' => 'test-channel',
'event' => 'some-event',
'data' => json_encode(['data' => 'yes']),
])
->seeJson([
'ok' => true,
'ok' => false,
]);

if (method_exists($this->channelManager, 'getPublishClient')) {
$this->channelManager
->getPublishClient()
->assertCalledWithArgs('publish', [
$this->channelManager->getRedisKey('1234', 'test-channel'),
json_encode([
'channel' => 'test-channel',
'event' => 'some-event',
'data' => ['data' => 'yes'],
'appId' => '1234',
'serverId' => $this->channelManager->getServerId(),
]),
]);
}
$this->markTestIncomplete(
'Broadcasting is not possible to be tested without receiving a Pusher error.'
);
}

public function test_cant_send_message_for_invalid_app()
{
$this->actingAs(factory(User::class)->create())
->json('POST', route('laravel-websockets.event'), [
'appId' => '9999',
'key' => 'TestKey',
'secret' => 'TestSecret',
'channel' => 'test-channel',
'event' => 'some-event',
'data' => json_encode(['data' => 'yes']),
Expand Down