Reputation: 4425
Is possible call a laravel event and send a variable in socket.io ?
This is the line that I want to know if is possible implement in socket sending the socket id:
socket.emit('App\\Events\\ClosePlaySession', socket.id);
Socket.js
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server, {'pingInterval': 2000, 'pingTimeout': 5000});
var Redis = require('ioredis');
var redis = new Redis();
io.set('heartbeat timeout', 10);
io.set('heartbeat interval', 4);
server.listen(3000);
io.on('connection', function (socket) {
console.log("new client connected " + socket.id);
socket.on('disconnect', function() {
socket.emit('App\\Events\\ClosePlaySession', socket.id); // This is possible?
console.log('client disconnected');
});
});
event ClosePlaySession.php
<?php
namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
// Repositories
use wtv\Repositories\PlaySessionRepository;
class ClosePlaySession extends Event implements ShouldBroadcast
{
use SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($socket_id)
{
$playSessionRepository = new PlaySessionRepository;
$playSessionRepository->closeSession($socket_id);
}
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return ['closePlaySession'];
}
}
Or what is the best way to send the socket id in order to update a record in the database.
Thanks
Upvotes: 1
Views: 2226
Reputation: 627
you can find on this two repositories of my own a fully functional example of what you want to do:
https://github.com/bretanac93/socket_io_events_laravel
https://github.com/bretanac93/backend_io_events_laravel
You need a Redis server running and to configure and install the dependencies in order you can test the project, the frontend is on the same repository of the backend, is a little Vue app, there I am showing in real time when people goes connected and disconnected, it uses the LoggedIn, LoggedOut, and RegisteredEvents, but you can define your own Events as well, hope works for you this little example, Bests! ;)
PD: Don't forget to star if you like it, and vote up the answer XD.
You can check in this link, the line marked, you'll find that I modify the user in the db: https://github.com/bretanac93/backend_io_events_laravel/blob/master/app/Listeners/SuccessfullLogin.php#L30
The explanation for that: You always have an event that is broadcasted, but you also can define an EventListener, when this event is called, you just have to fire this Listener, and everything ok, here, that is translated to listen when a User logs in, signs up or logs out (in this project at least), if any of those actions happens, fire their own Listener defined on your EventServiceProvider.php
, like in here: https://github.com/bretanac93/backend_io_events_laravel/blob/master/app/Providers/EventServiceProvider.php#L16
Note that you are using your socket only for communicating to your frontend when some of these actions happens, and the backend is in charge of fire the correct events listening the action is called right now (i.e: In a chat room, you want to reflex when a user goes online or offline, in the backend you define two Listeners, one for Log in and the other for Logout, when one of those things happens, is been called an Event that is defined as Broadcast, the socket sends that signal from backend to frontend, but stores the change in the db when you send that signal, and in the frontend, you change in DOM a message between offline or online).
Hope that you understands nice now, if you need some more help or if there is something also you need, don't hesitate to comment. Cheers! ;)
Upvotes: 1