-
Notifications
You must be signed in to change notification settings - Fork 656
Exist a way to implement webhooks? #80
Comments
I also want this! |
This would be really cool but possibly also tricky looking at the docs it has a few "specialties" that requires some work to make this work correctly. For example: https://pusher.com/docs/webhooks#delay Just spit-ballin' here, but it might be a good one to write events to an Redis buffer/list and let another process/cron handle the actual sending to prevent adding to much work to the websocket server process causing it to block more than needed (which also would be an easy-ish way to get batched hook working easily). |
@stayallive So basically the main feature to be develop here could be just an option to save events somewhere (like redis), and each individual project cares about what to do with the stored events? |
@joaokamun well, I would implement a |
@enzonotario could you please share an example of what you've done? Thanks! Update: Managed to get it done by extending the |
@stefandanaita sorry for the delay... I just have registered a singleton in my
Router.php
WebSocketHandler.php
but sure, this is a weird way. Extending as you said in #21 I think that is a better way! |
@stefandanaita You sir are BRILLIANT!!!!!!!!!! This was a lifesaver!!! Thank you so much. Quick question though, do you know i can know which client disconnect from the app when the connection is closed? That's the whole point of extending this package. I need to tell the chat app he is offline after a non graceful log out. Thanks. |
@nicolasvahidzein Did you figured out how to identify user once we extend the WebSocketHandler? |
Hi, |
@noobshooter27 The connection can make use of the channel manager and you can get the connection from there. |
2.x will contain extendable hooks: #465 |
Hi @rennokki , may I know how can I get the private channel name from the |
@hengjingyoong |
@hengjingyoong Have you called |
@rennokki, I just used the original WebSocketHandler to test it out. Thanks @noobshooter27 , finally I managed to find out what is the channel name from the closing connection. What I did was defined a custom function in public function checkConnection($socketId)
{
if ($this->subscribedConnections[$socketId] ?? null) {
return $this->channelName;
}
return false;
} and then in public function onClose(ConnectionInterface $connection)
{
$allChannels = $this->channelManager->getChannels($connection->app->id);
foreach($allChannels as $channel){
if ($channelName = $channel->checkConnection($connection->socketId)) {
break;
}
}
dd($channelName);
$this->channelManager->removeFromAllChannels($connection);
DashboardLogger::disconnection($connection);
StatisticsLogger::disconnection($connection);
} But I'm not sure if that is secure to work in this way. ps: my working scenario is attach the user_id to the private channel name, that's why I need to get back the user_id from channel name, and notify my backend which user is offline. This is to handle the |
@hengjingyoong It's highly recommended to not change any |
Do you have any though on getting the channel name without adding my own function in |
Finally I figure out how to get the closing channel name. Here is it. <?php
namespace App\WebSocket;
use Ratchet\ConnectionInterface;
use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler as BaseWebSocketHandler;
class CustomWebSocketHandler extends BaseWebSocketHandler
{
public function onClose(ConnectionInterface $connection)
{
$allChannels = $this->channelManager->getChannels($connection->app->id);
foreach($allChannels as $channelName => $channel){
if($channel->getSubscribedConnections()[$connection->socketId] ?? null) {
$closingChannel = $channel->getChannelName();
break;
}
}
dd($closingChannel);
parent::onClose($connection);
}
} ps: I was missed out the |
Thank you @hengjingyoong! <?php
namespace App\WebSocket;
use Ratchet\ConnectionInterface;
use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler as BaseWebSocketHandler;
class CustomWebSocketHandler extends BaseWebSocketHandler
{
public function onClose(ConnectionInterface $connection)
{
$allChannels = $this->channelManager->getChannels($connection->app->id);
$closingChannel = null;
foreach($allChannels as $channelName => $channel){
if($channel->getSubscribedConnections()[$connection->socketId] ?? null) {
$closingChannel = $channelName;
break;
}
}
dd($closingChannel);
parent::onClose($connection);
}
} |
Hi thank you for your code but I need users data inside of $allChannels how can i get it? |
Another approach that is simple is using the custom ArrayChannelManager as indicated in the websockets config, and then make a call like: public function removeFromAllChannels(ConnectionInterface $connection)
{
// THIS ONE LINE
$response = Http::get(config('app.url') . '/socket-disconnected/' . $connection->socketId . '/{key-for-security}');
if (!isset($connection->app)) {
return;
}
....
} In web.php: use App\Http\Controllers\WebsocketController;
Route::get('/socket-disconnected/{socket_id}/{security_key}', [WebsocketController::class, 'socketDisconnected']); Dispatch the desired job in WebsocketController: class WebsocketController extends Controller
{
public function socketDisconnected(Request $request, string $socket_id, string $security_key)
{
logger("Socket id received: " . $socket_id);
if($security_key == config('websockets.security_key')) {
ProcessWebsocketDisconnects::dispatch($socket_id);
}
return response('OK', 200)->header('Content-Type', 'text/plain');
}
} |
Thank you @garrettboone I will have to try this soon. Sounds awesome. |
The only missing feature for me fully migrate from pusher is the event webhooks, I could not find anything about it. Is something planned to have? I can contribute to that, just want to know if you guys have something in mind or some direction to follow.
Regards,
The text was updated successfully, but these errors were encountered: