Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want to implement web sockets in Laravel

I want to implement web notifications in Laravel using web sockets. I have tried pusher. It is very easy but it is a paid package. I have also tried redis and socket.io for which I have to install horizon. I am running Windows and I cannot install it on Windows according to what I have read about horizon.

So I am very confused about push notification. I am trying for at least one week but haven't found any solution yet. My front end is in angluar 5/android and backend is in Laravel 5.6.

Can you please suggest me any good ideas to implement push notification?

like image 238
Pranav Mandlik Avatar asked May 29 '18 04:05

Pranav Mandlik


People also ask

How is WebSocket implemented in Laravel?

Installing Laravel WebSocketsThe above publishes the WebSockets configuration file to your application's config/websockets. php file. In there you can configure the WebSockets server settings. For more information about the configuration file, you can check the documentation.

Does Laravel support WebSockets?

The laravel-websockets and soketi packages provide Pusher compatible WebSocket servers for Laravel. These packages allow you to leverage the full power of Laravel broadcasting without a commercial WebSocket provider.

Can I use WebSockets in PHP?

The WebSocket is used to create a bridge to send or receive messages from the PHP chat server. In the web world, we generally use HTTP request methods to communicate between the client and server side. In this chat example, we use sockets to communicate with the server.


4 Answers

Take a look at this article. It covers implementing websocket in laravel using redis driver, Laravel echo and socket.io client.

According to above article:

“Why don’t you just use Pusher?”

Here is the thing.

Laravel comes with Pusher enabled. Even though Pusher seems like a quick “Plug and play” solution (which it is), it comes with limitations. Check https://pusher.com/pricing

And most of the tutorials trick you with their title of implementing Websockets when in reality they just want to give you Pusher. (And my favorite part is when they say that you can easily switch to socket.io)

like image 136
Khalil Laleh Avatar answered Oct 18 '22 02:10

Khalil Laleh


You have been use pusher,so I assume you know how to use event.

I recommend you laravel-echo-server.It's very easy to use,with a built-in api.

Here is a fresh example of a laravel-echo project from scratch. After you set up the project.

you will need predis if you haven't install it yet

composer require predis/predis
redis-server /usr/local/etc/redis.conf
  1. run npm install -g laravel-echo-server
  2. run laravel-echo-server init
  3. uncomment App\Providers\BroadcastServiceProvider::class in config/app.php
  4. add <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/socket.io.js"></script> in your app.blade.php
  5. you need a socketio-client ,you can run npm install --save laravel-echo .In case you are not familiar with npm or vue, you can simply include this compiled file from my github project.compiledjs
  6. add this to app.blade.php

    <script>
    window.Echo = new Echo({
    broadcaster: 'socket.io',
    
    host: '{{url('/').':6001'}}',
    });
    Echo.private(`App.User.{{Auth::id()}}`) // private channel
    .listen('NewMessage', (e) => {
        console.log(e)
    
    });
    
    
    </script>
    
  7. finally run laravel-echo-server start and open you project ,you will see

    [20:53:21] - Lv5OKDAcuSLsK1nBAAAE authenticated for: private- 
       App.User.1
    [20:53:21] - Lv5OKDAcuSLsK1nBAAAE joined channel: private-App.User.1
    
  8. you can listen any event you want or other notifications

  9. here is my github project echo-example

add a little code so that you can use postman send message to specfic user.You can see the message from chrome console.You can get more details from github project screenshots

like image 6
Evol Rof Avatar answered Oct 18 '22 02:10

Evol Rof


As per laravel documentation, I will recommend using Pusher. I have created a package for to user WebSockets in laravel.

https://www.techzonemind.com/scalable-websocket-server-laravel-applications/

It will use Redis for background queuing to optimize performance. I have used it in few solutions. I may not be usable in all use cases. But good to have a look.

PHP not build to execute concurrent tasks, if you have a choice, nodejs will be a better solution,

like image 2
Jithin Jose Avatar answered Oct 18 '22 01:10

Jithin Jose


THE BEST WAY TO INTEGRATE REALTIME IN LARAVEL IS NIETHER NODE NOR PUSHER

You find Pusher Easier and you don't want to pay the price . Then without hesitation you should try Laravel Websockets . Its a very simple Pusher Replacement .

In Short You use the pusher client libs to access a pusher like server that is hosted in your php server . Since its open source its free . Its better than using socket.io or redis because they require you to pay additional server costs . You can check their demo app here

PS

If you want more custom controls then you can use ratchet

like image 1
Saket Aryan Avatar answered Oct 18 '22 00:10

Saket Aryan