laravel/reverb external connection
Hello,
I am writing a java program that connects to a laravel reverb server. I managed to get a connection, laravel reverb reads the message but does not broadcast it to the appropriate channel.
No Broadcasting To ................................... App.Models.WeightScale.1 in debug.
However, if I call this event manually in laravel I get this debug.
The channel is not private or anything like that. I join the channel. If I send the event directly in laravel my java application reads this event correctly.
Please help, the documentation on this issue is scarce.
{"event":"pusher_internal:subscription_succeeded","data":"{}","channel":"App.Models.WeightScale.1"}
My simple event:
{"data":{"weight":123.56},"channel":"App.Models.WeightScale.1","event":"WeightScaleWeightUpdated"}
frontend code:
useEffect(() => {
Echo.channel('App.Models.WeightScale.1').listen('.WeightScaleWeightUpdated', (e) => {
console.log(e);
});
}, []);
event:
<?php
namespace App\Modules\WasteManagement\WeightScale\Application\Events;
use App\Modules\WasteManagement\WeightScale\Application\Models\WeightScale;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class WeightScaleWeightUpdated implements ShouldBroadcast
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;
public function __construct(
private WeightScale $weightScale
) {
}
public function broadcastOn()
{
return new Channel('App.Models.WeightScale.' . $this->weightScale->id);
}
public function broadcastWith()
{
return [
'weight' => $this->weightScale->weight,
];
}
public function broadcastAs(): string
{
return 'WeightScaleWeightUpdated';
}
}
debug endpoint in laravel:
Route::get('/test', function() {
/* @var WeightScale $ws */
$ws = WeightScale::find(1);
$ws->setWeight(123.45);
});
setWeightFunction:
public function setWeight(float $weight)
{
$this->weight = $weight;
event(new WeightScaleWeightUpdated($this));
}
java class:
package com.mycompany.mavenproject1;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import java.net.URI;
import org.json.JSONObject;
public class ReverbClient extends WebSocketClient {
public ReverbClient(URI serverUri) {
super(serverUri);
}
@Override
public void onOpen(ServerHandshake handshakedata) {
System.out.println("Połączono z serwerem Reverb");
MainJFrame.isServer = true;
}
@Override
public void onMessage(String message) {
JSONObject messageJSON = new JSONObject(message);
if (messageJSON.has("event")) {
if (messageJSON.get("event").equals("pusher:ping")) {
this.sendEvent("pusher:pong", new JSONObject());
}
}
System.out.println("Otrzymano wiadomość: " + message);
}
@Override
public void onClose(int code, String reason, boolean remote) {
System.out.println("Połączenie zamknięte: " + reason);
}
@Override
public void onError(Exception ex) {
ex.printStackTrace();
}
public void subscribeToChannel(String channelName) {
JSONObject subscribeMessage = new JSONObject();
subscribeMessage.put("event", "pusher:subscribe");
JSONObject data = new JSONObject();
data.put("auth", "");
data.put("channel", channelName);
subscribeMessage.put("data", data);
this.send(subscribeMessage.toString());
System.out.println("Wysłano żądanie subskrypcji do kanału: " + channelName);
}
public void sendEvent(String eventName, JSONObject data) {
JSONObject json = new JSONObject();
json.put("event", eventName);
json.put("channel", "App.Models.WeightScale.1");
json.put("data", data);
this.send(json.toString());
System.out.println("Wysłano event: " + json.toString());
}
}
my channel:
Broadcast::channel('App.Models.WeightScale.{id}', function() {
return true;
});
Please or to participate in this conversation.