Hamza ait sidi said

Me

``I got a captcha wrong once, so now I'm a robot``.
Hi, my name is Hamza but my friends love's to call me TOPO,I am a 28-year-old full-stack developer from Morocco. I specialize in Laravel, Livewire, and AlpineJS. I fell in love with PHP and Laravel when I was 24. When I'm not coding, I enjoy exploring topics such as rap music, religion, red pill philosophy, and tuffting.

Laravel Observers
PHP 8
PUSHER

Read This

Deploying Your Laravel Application on Heroku

By Hamza Ait Sidi Said

Heroku makes it easy to deploy and manage Laravel applications in a cloud environment. In this guide, we'll walk through the steps to deploy your Laravel app on Heroku using the Heroku Command Line Interface (CLI).

Prerequisites

Before you begin, ensure you have the following:

  • A Heroku account (sign up at Heroku Sign Up).
  • Heroku CLI installed (download it from Heroku CLI).
  • Your Laravel application code hosted on a version control system (e.g., Git).

Deployment Steps

Follow these steps to deploy your Laravel application on Heroku:

  1. Create a Heroku App: In your terminal, navigate to your Laravel project directory and run the following command to create a new Heroku app:
  2. heroku create your-app-name
  3. Configure Environment Variables: If your Laravel app uses environment variables (e.g., for database credentials), set them on Heroku:
  4. heroku config:set APP_KEY=$(php artisan --no-ansi key:generate --show)

    Replace your-app-name with your desired app name.

  5. Set the Application Environment: Set the environment to production on Heroku:
  6. heroku config:set APP_ENV=production
  7. Add a Procfile: Create a file named Procfile (without any file extension) in your Laravel project root with the following content:
  8. web: vendor/bin/heroku-php-apache2 public/
  9. Commit Your Changes: Make sure your changes are committed to your version control system (e.g., Git).
  10. Deploy Your Application: Deploy your Laravel application to Heroku:
  11. git push heroku master
  12. Run Migrations: Run database migrations on Heroku:
  13. heroku run php artisan migrate
  14. Open Your App: Once the deployment is complete, open your app in the browser:
  15. heroku open

Conclusion

Congratulations! Your Laravel application is now live on Heroku. Heroku's simplicity and scalability make it an excellent choice for hosting and managing your Laravel projects in a cloud environment.

Real-Time Communication with Pusher WebSocket and Laravel

By Hamza Ait Sidi Said

Real-time communication has become a crucial aspect of modern web applications. Laravel, a powerful PHP framework, makes it easy to integrate real-time features using Pusher, a hosted WebSocket service. In this tutorial, we'll explore the steps to set up and use Pusher WebSocket with a Laravel application.

Prerequisites

Before you begin, ensure you have the following:

  • A Laravel project set up and running.
  • A Pusher account (sign up at Pusher).

Setting Up Pusher in Laravel

Follow these steps to integrate Pusher with your Laravel application:

  1. Install the Pusher PHP Library: Run the following command in your terminal:
  2. composer require pusher/pusher-php-server
  3. Configure Pusher Credentials: Add your Pusher credentials to the .env file:
  4. PUSHER_APP_ID=your-app-id
      PUSHER_APP_KEY=your-app-key
      PUSHER_APP_SECRET=your-app-secret
      PUSHER_APP_CLUSTER=your-app-cluster
  5. Install Laravel Echo and Pusher JavaScript SDK: Run the following commands:
  6. npm install --save laravel-echo pusher-js
  7. Configure Broadcasting: Set the broadcasting driver to Pusher in the config/broadcasting.php file:
  8. 'default' => env('BROADCAST_DRIVER', 'pusher'),
  9. Create an Event: Generate an event using the artisan command:
  10. php artisan make:event YourEvent
  11. Update Event Logic: Open the generated event file in app/Events/YourEvent.php and define the logic to broadcast:
  12. public function broadcastOn()
      {
          return new Channel('your-channel');
      }
  13. Broadcast the Event: Trigger the event where needed in your application:
  14. event(new YourEvent($data));

Listening to Events with Laravel Echo

Use Laravel Echo to listen to events on the client side:

  1. Install Laravel Echo and Pusher JavaScript SDK: If not done already, run the following commands:
  2. npm install --save laravel-echo pusher-js
  3. Configure Laravel Echo: In your JavaScript file (e.g., resources/js/bootstrap.js), configure Laravel Echo:
  4. import Echo from 'laravel-echo';
      
      window.Pusher = require('pusher-js');
      
      window.Echo = new Echo({
          broadcaster: 'pusher',
          key: process.env.MIX_PUSHER_APP_KEY,
          cluster: process.env.MIX_PUSHER_APP_CLUSTER,
          forceTLS: true,
      });
  5. Listen for Events: In your Vue component or JavaScript file, listen for the events:
  6. mounted() {
          Echo.channel('your-channel')
              .listen('YourEvent', (data) => {
                  console.log('Event received:', data);
                  // Handle the event data as needed
              });
      }

Conclusion

Congratulations! You've successfully set up Pusher WebSocket with your Laravel application. Real-time communication opens up possibilities for interactive and dynamic user experiences in your web applications.

dark_mode light_mode
twitter github linkedin