Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Mongodb Laravel Package

enqueue/mongodb

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require enqueue/mongodb
    

    Ensure MongoDB is running locally or accessible via connection string.

  2. Basic Configuration:

    use Enqueue\MongoDB\MongoDBConnectionFactory;
    use Enqueue\Client\Producer;
    
    $connection = MongoDBConnectionFactory::createConnection(
        'mongodb://localhost:27017',
        'enqueue'
    );
    $producer = new Producer($connection);
    
  3. First Use Case:

    $producer->send(new Message('Hello MongoDB Queue!'));
    

Key Files to Review


Implementation Patterns

Workflow Integration

  1. Producer Workflow:

    • Use Producer to dispatch jobs to MongoDB collections (e.g., jobs, delayed_jobs).
    • Example:
      $producer->send(new Message('task', ['data' => 'payload']));
      
  2. Consumer Workflow:

    • Use Consumer to process messages from MongoDB:
      $consumer = new Consumer($connection);
      $consumer->consume(function (Message $message, Context $context) {
          // Process message
          $context->ack();
      });
      
  3. Delayed Jobs:

    • Leverage MongoDB’s delayed_jobs collection for scheduled tasks:
      $producer->send(new Message('delayed_task', ['delay' => 3600])); // Delay 1 hour
      
  4. Error Handling:

    • Implement retry logic via Context:
      $context->reject($message, new Exception('Failed'), 3); // Retry 3 times
      

Laravel-Specific Patterns

  1. Service Provider:

    use Enqueue\MongoDB\MongoDBConnectionFactory;
    use Illuminate\Support\ServiceProvider;
    
    class MongoDBQueueServiceProvider extends ServiceProvider {
        public function register() {
            $this->app->singleton('mongodb.connection', function () {
                return MongoDBConnectionFactory::createConnection(
                    config('queue.mongodb.connection'),
                    config('queue.mongodb.database')
                );
            });
        }
    }
    
  2. Queue Configuration:

    'queue' => [
        'connections' => [
            'mongodb' => [
                'driver' => 'mongodb',
                'connection' => 'mongodb://localhost:27017',
                'database' => 'enqueue',
                'queue' => 'default', // Optional: Custom collection name
            ],
        ],
    ],
    
  3. Job Dispatch:

    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Queue\ShouldQueue;
    
    class ProcessOrder implements ShouldQueue {
        use Queueable;
    
        public function handle() {
            // Job logic
        }
    }
    
    ProcessOrder::dispatch();
    

Gotchas and Tips

Pitfalls

  1. Connection Management:

    • Ensure MongoDB is accessible and the connection string is correct. Test with:
      mongo --host localhost --port 27017 --eval 'db.runCommand({ping: 1})'
      
    • Avoid hardcoding credentials; use Laravel’s .env or config files.
  2. Collection Naming:

    • Default collections (jobs, delayed_jobs) must exist. Create them manually if needed:
      $collection = $connection->getCollection('jobs');
      $collection->createIndex(['priority' => -1, 'delay' => 1]);
      
  3. Message Serialization:

    • MongoDB has size limits (~16MB per document). For large payloads, use external storage (e.g., S3) and store only a reference in the message.
  4. Concurrency Issues:

    • MongoDB’s single-document atomicity can cause race conditions. Use findAndModify or transactions (MongoDB 4.0+) for critical operations.

Debugging Tips

  1. Log Messages: Enable debug logging for the enqueue/mongodb package:

    $connection->setLogger(new Monolog\Logger('mongodb', [new Monolog\Handler\StreamHandler('storage/logs/mongodb.log')]));
    
  2. Check Collections:

    • Inspect collections directly in MongoDB Compass or CLI:
      mongoexport --db enqueue --collection jobs --out jobs.json
      
  3. Common Errors:

    • OperationFailure: Likely a connection issue or invalid collection name.
    • Timeout: Increase MongoDB’s socketTimeoutMS or check network latency.

Extension Points

  1. Custom Collections: Override default collections via MongoDBConnectionFactory:

    $connection = MongoDBConnectionFactory::createConnection(
        'mongodb://localhost:27017',
        'enqueue',
        'custom_jobs', // Custom queue collection
        'custom_delayed' // Custom delayed collection
    );
    
  2. Middleware: Add processing logic via Middleware:

    $connection->addMiddleware(new class implements Middleware {
        public function handle(Message $message, Context $context, callable $next) {
            // Pre-process
            $result = $next($message, $context);
            // Post-process
            return $result;
        }
    });
    
  3. Bulk Operations: Use MongoDB’s bulk write API for efficiency:

    $bulk = new \MongoDB\Driver\BulkWrite;
    $bulk->insert([new Message('task1'), new Message('task2')]);
    $connection->getCollection('jobs')->executeBulkWrite($bulk);
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
codifyo/ts-generator-bundle
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor