michaelthedev / rest-task

Laravel 11 Task Manager REST API with WebSocket (Broadcast)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TaskMan

About

This project is my attempt at the Niyo Group task

Features

  1. User Authentication
    • Registration
    • Login
    • JWT API token
    • Refresh token
    • Logout
  2. Task Management
    • Create a task
    • Update a task
    • Delete a task
    • Get all tasks
    • Get a task by unique id
  3. Real-time updates using websockets

Installation

  1. Prerequisites
    • PHP 8.3+
    • Laravel 11
    • Composer
    • Node.js (for websocket setup)
    • MySQL
  2. Clone this repository
    git clone https://github.com/michaelthedev/rest-task.git
    
    cd rest-task
  3. Install dependencies
    composer install
    npm install
  4. Set up environment file
    cp .env.example .env
    Replace the database credentials in the .env file with your own
  5. Generate application key
    php artisan key:generate
  6. Run the migrations
    php artisan migrate
  7. Regenerate JWT secret key (for API)
    php artisan jwt:secret
  8. Start the development server
    php artisan serve

API endpoints

API Documentation

POSTMAN>> https://documenter.getpostman.com/view/10657913/2sA3QmEaG2

Routes/Endpoints

Method Endpoint Description
POST /auth/login Login a user
POST /auth/register Register a user
POST /auth/logout Logout a user (with authorization)
POST /auth/refresh Refresh a user token (with authorization)
GET /auth/user Get the authenticated user (with authorization)
POST /tasks Create a task (with authorization)
GET /tasks Get all tasks (with authorization)
GET /tasks/{uid} Get a task by unique id (uid) (with authorization)
PUT /tasks/{uid} Update a task
DELETE /tasks/{uid} Delete a task

Websockets and Real-time Updates

By default, the application runs Laravel Reverb on localhost:8080. These can be configured further in the .env file. The web socket can be used outside laravel as well.

More documentation about broadcasting and events in Laravel is available on Laravel's documentation. You can also check the Laravel Reverb documentation

Make sure to run this to regenerate your Reverb keys (on your laravel project)

php artisan reverb:install

Follow this process to set up web sockets (whether in Laravel or outside Laravel)

  1. Install Laravel Echo and Pusher
      npm install --save laravel-echo pusher-js
  2. Set up Laravel Echo
    import Echo from 'laravel-echo';
    import Pusher from 'pusher-js';
    
    window.Pusher = Pusher;
    window.Echo = new Echo({
     broadcaster: 'reverb',
     key: {{REVERB_APP_KEY}},
     wsHost: {{REVERB_HOST}},
     wsPort: {{REVERB_PORT}},
     wssPort: {{REVERB_PORT}},
     forceTLS: {{https or http}},
     enabledTransports: ['ws', 'wss'],
     auth: {
         headers: {
             Authorization: 'Bearer JWT_TOKEN_HERE'
         }
     }
    });
  3. Listen for events
    const userId = 1; // replace with actual user id
    window.Echo.private('App.Models.User.'+userId)
        .listen('TaskAdded', (e) => {
            console.log("TaskAdded event received")
            console.log(e)
        })
        .listen('TaskUpdated', (e) => {
            console.log("TaskUpdated event received")
            console.log(e)
        })
        .listen('TaskDeleted', (e) => {
            console.log("TaskUpdated event received")
            console.log(e)
        })
  4. Start laravel, queue and reverb
    php artisan serve
    php artisan queue:listen
    php artisan reverb:start
  5. See resources/js/echo.js for a sample implementation. You can also run npm run dev for the websocket sample

Testing

This project uses phpunit for feature tests. Run the following command to run the tests

php artisan test

Test screenshot

resize

Author

Name: Michael Arawole.

Email: michael@logad.net

About

Laravel 11 Task Manager REST API with WebSocket (Broadcast)


Languages

Language:PHP 70.4%Language:Blade 28.5%Language:JavaScript 1.0%