Status message

Maintenant, vous regardez: Vue, Laravel echo + socket.io + lumen(event, listener)

Vue, Laravel echo + socket.io + lumen(event, listener)

一: Laravel echo + Vue
1. First install socket-client, laravel-echo package for vuejs

  1. $ yarn add socket.io-client laravel-echo

Then the package file will be like this:
package.json file:

  1. "dependencies": {
  2. "axios": "^0.18.0",
  3. "bootstrap": "^4.0.0-beta.2",
  4. "bootstrap-vue": "latest",
  5. "cropperjs": "^1.4.3",
  6. "jquery": "^3.3.1",
  7. "laravel-echo": "^1.4.0",
  8. "moment": "^2.22.2",
  9. "popper.js": "^1.12.9",
  10. "pusher-js": "^4.3.0",
  11. "socket.io-client": "^2.2.0",
  12. "style-loader": "^0.21.0",
  13. "sweetalert2": "^7.28.4",
  14. "vue": "^2.5.17",
  15. "vue-axios": "^2.1.4",
  16. "vue-loading-spinner": "^1.0.11",
  17. "vue-pdf": "^3.3.1",
  18. "vue-router": "^3.0.1",
  19. "vue-upload-component": "^2.8.14",
  20. "vuex": "^3.0.1"
  21. },

Registering these two package to src/main.js file :

  1. import Echo from 'laravel-echo'
  2. window.io = require('socket.io-client')
  3.  
  4. window.Echo = new Echo({
  5. broadcaster: 'socket.io',
  6. host: '<a href="http://localhost:6001',
  7. ">http://localhost:6001',
  8. </a> auth: {headers: {Authorization: 'Bearer ' + localStorage.getItem('token')}}
  9. })

Applying in Footer.vue file

  1. methods: {
  2. getEventsNoFromSocket () {
  3. let currentUser = JSON.parse(localStorage.getItem('user'))
  4. window.Echo.channel('parseNoti.' + currentUser.id)
  5. .listen('.App\\Events\\ParseFilesEvent', (e) => {
  6. this.messages.push(e)
  7. })
  8. },
  9. mounted () {
  10. this.getEventsNoFromSocket()
  11. }
  12. }

二. Laravel-echo-server:
Install laravel-echo-server:

  1. $ npm install -g laravel-echo-server
  2. $ laravel-echo-server init //generate a laravel-echo-server.json file
  1. {
  2. "authHost": "<a href="http://localhost:8000",
  3. ">http://localhost:8000",
  4. </a> "authEndpoint": "/broadcasting/auth",
  5. "clients": [],
  6. "database": "redis",
  7. "databaseConfig": {
  8. "redis": {},
  9. "sqlite": {
  10. "databasePath": "/database/laravel-echo-server.sqlite"
  11. }
  12. },
  13. "devMode": true,
  14. "host": null,
  15. "port": "6001",
  16. "protocol": "http",
  17. "socketio": {},
  18. "sslCertPath": "",
  19. "sslKeyPath": "",
  20. "sslCertChainPath": "",
  21. "sslPassphrase": "",
  22. "subscribers": {
  23. "http": true,
  24. "redis": true
  25. },
  26. "apiOriginAllow": {
  27. "allowCors": true,
  28. "allowOrigin": "*",
  29. "allowMethods": "",
  30. "allowHeaders": ""
  31. }
  32. }

Run socket server:

  1. $ laravel-echo-server start

Using supervisor to run socket server

  1. sudo apt-get install supervisor // install supervisor

//create a echo-server.conf file within /etc/supervisor/conf.d folder

  1. [program:echo-server]
  2. process_name=%(program_name)s_%(process_num)02d
  3. command=/usr/bin/laravel-echo-server start --dir=/var/www/html/echo
  4. autostart=true
  5. autorestart=true
  6. user=root
  7. redirect_stderr=true
  8. stdout_logfile=/home/echo-server.log

Then run this scripts:

  1. sudo supervisorctl reread
  2. sudo supervisorctl update
  3. sudo supervisorctl start echo-server:*

三:Lumen event listener

  1. // event. channel, listener
  2. namespace App\Events;
  3.  
  4. use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
  5. use Illuminate\Broadcasting\Channel;
  6. use Illuminate\Broadcasting\PrivateChannel;
  7. use Illuminate\Broadcasting\PresenceChannel;
  8. use Illuminate\Broadcasting\InteractsWithSockets;
  9. use Illuminate\Support\Carbon;
  10.  
  11. class ParseFilesEvent extends Event implements ShouldBroadcast
  12. {
  13.  
  14. public $message;
  15.  
  16. public $user_id;
  17.  
  18. public $noti_time;
  19.  
  20. public $noti_type;
  21.  
  22. /**
  23.   * The name of the queue on which to place the event.
  24.   *
  25.   * @var string
  26.   */
  27. // public $broadcastQueue = 'uploadResultNotification';
  28.  
  29. /**
  30.   * Create a new event instance.
  31.   *
  32.   * @return void
  33.   */
  34. public function __construct($user_id, $message, $noti_type = 1 )
  35. {
  36. //
  37. $this->user_id = $user_id;
  38. $this->message = $message;
  39. $this->noti_type = $noti_type;
  40. $this->noti_time = Carbon::now()->format('Y-m-d H:i:s');
  41. }
  42.  
  43. /**
  44.   * Get the channels the event should broadcast on.
  45.   *
  46.   * @return array
  47.   */
  48. public function broadcastOn()
  49. {
  50. return new Channel('parseNoti.' . $this->user_id);
  51. }
  52.  
  53. }
  54.  
  55. //Listener
  56. namespace App\Listeners;
  57.  
  58. use App\Events\ParseFilesEvent;
  59. use Illuminate\Queue\InteractsWithQueue;
  60. use Illuminate\Contracts\Queue\ShouldQueue;
  61. use Log;
  62. use Illuminate\Support\Facades\Notification;
  63. use App\Notifications\ParseFilesNoti;
  64. use App\User;
  65.  
  66. class ParseFilesListener
  67. {
  68. /**
  69.   * Create the event listener.
  70.   *
  71.   * @return void
  72.   */
  73. public function __construct()
  74. {
  75. //
  76. }
  77.  
  78. /**
  79.   * Handle the event.
  80.   *
  81.   * @param ExampleEvent $event
  82.   * @return void
  83.   */
  84. public function handle(ParseFilesEvent $event)
  85. {
  86. //
  87. //Log::info('Event is happing: ' . $event->message);
  88. Notification::send(User::find($event->user_id), new ParseFilesNoti($event->message));
  89. }
  90. }
  1. // fire an event
  2. Event::dispatch(new ParseFilesEvent($this->current_user_id , $message ));
  1. # /etc/supervisor/conf.d/laravel-worker.conf
  2. [program:laravel-worker]
  3. process_name=%(program_name)s_%(process_num)02d
  4. #command=php /var/www/html/cats/cats-backend/artisan queue:work --timeout=30 --sleep=20 --tries=1
  5. command=php /var/www/html/hydcats-backend/artisan queue:work --timeout=30 --sleep=20 --tries=1
  6. autostart=true
  7. autorestart=true
  8. user=root
  9. numprocs=8
  10. redirect_stderr=true
  11. stopasgroup=true
  12. stopsignal=QUIT
  13. stdout_logfile=/home/worker.log

Comments

Great article!

Hi it's me, I am also visiting this web site on a regular basis, this website is really pleasant and the viewers

are truly sharing fastidious thoughts.

pond lily (Lorrie)

Appreciation to my father who shared with me regarding this weblog, this blog is actually

remarkable.

rhinalgia [Alberto]

I visited many blogs but the audio quality

for audio songs existing at this web page is

actually marvelous.

probation (Gerardo)

excellent post, very informative. I ponder why the opposite experts of this sector do not

understand this. You should proceed your writing.

I am sure, you've a huge readers' base already!

Bryanthus taxifolius (Coy)

Ahaa, its pleasant conversation on the topic of this paragraph

here at this weblog, I have read all that, so at this time me also commenting at

this place.

utmosts (Clemmie)

Everything is very open with a very clear explanation of the issues.

It was truly informative. Your site is very useful.

Thank you for sharing!

Republic of Kazakhstan [Roslyn]

https://creditos-personales.blogspot.com/2019/11/prestamos-trading-fidin...

I do not even know how I ended up here, but I thought this post was great.

I don't know who you are but certainly you are going to a famous blogger if you aren't already ;) Cheers!

confusability (Layla)

https://creditos-personales.blogspot.com/2019/11/prestamos-trading-fidin...

Really no matter if someone doesn't be aware of then its up

to other users that they will assist, so here it happens.

genus Zaglossus (Raymon)

https://virtualinternetandbusinessonline.blogspot.com/2019/11/los-presta...

I have read so many posts concerning the blogger lovers except this

piece of writing is truly a pleasant piece

of writing, keep it up.

George Dewey (Anderson)

https://freexboxlivecodes2016.blogspot.com/2019/11/alternativas-para-que...

Excellent site. A lot of helpful information here. I am sending it to a few friends ans

also sharing in delicious. And of course, thanks to your sweat!

paragonimiases (Charity)

https://scrappleworks.blogspot.com/2019/11/que-hacer-si-estas-en-asnef-y...

Great post! We are linking to this great content on our site.

Keep up the good writing.

podsolic (Camilla)

https://andysmie.blogspot.com/2019/11/asi-son-los-prestamos-rapidos-onli...

Link exchange is nothing else but it is just placing the other person's webpage link on your page

at appropriate place and other person will also do same

in support of you.

recolouring [Steffen]

https://andysmie.blogspot.com/2019/11/asi-son-los-prestamos-rapidos-onli...

It's really a nice and useful piece of information. I

am satisfied that you shared this useful information with

us. Please stay us informed like this. Thanks for sharing.

headwards (Jane)

https://scrappleworks.blogspot.com/2019/11/que-hacer-si-estas-en-asnef-y...

Hello, this weekend is good designed for me, for the reason that this time i am reading this great

educational article here at my residence.

immatures (Valentina)

https://cristina-nuestraclase.blogspot.com/2019/11/que-hacer-cuando-se-a...

http://kb-direct.pl http://meblelobos.pl http://xannsem.com http://teldomains.pl http://teldomains.pl http://multi-mac.pl http://034548.org http://xannstat.com http://web7group.com http://rocela.pl http://web7group.com http://wanguardpr.pl http://fcbu.org http://meblenaogrod.com.pl http://merikotka.com Nothingness could be recognized after that as that within which despite, power,

room, time, or other type of information can exist, which

would be possible if symmetry and structure break within the manifold of the multiverse (i.e.

the manifold would certainly have openings or

splits).

Stop by my page - infowiesci.com.pl

Pages

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
7 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.