Follow these steps to configure Laravel Nightwatch in your Laravel application and run the agent as a background process in your Cloud environment. For more details, see the Nightwatch Getting Started Guide.

Configure Nightwatch on Cloud

1

Install Nightwatch package

Install the Laravel Nightwatch package in your application locally using Composer:

composer require laravel/nightwatch

Run composer require laravel/nightwatch on your local machine to update your composer.json and composer.lock files before deploying.

2

Add your Nightwatch token

Add your Nightwatch environment’s NIGHTWATCH_TOKEN to your .env file:

NIGHTWATCH_TOKEN=your-api-key

You will find this value in your Nightwatch dashboard when you create an application.

3

Configure the agent as a background process

To ensure the Nightwatch agent is always running and sending data, configure it as a background process in your Cloud environment:

  1. Go to your App cluster in the Cloud dashboard.

Select the App cluster

  1. Click New background process.

Select “New Background Process”

  1. Choose Custom worker.

Add a custom worker that runs the `nightwatch:agent` command

  1. Enter the following command:
php artisan nightwatch:agent
  1. Save your changes and redeploy your application.

If you’re using a dedicated worker cluster, repeat these steps for the worker cluster so it also runs the agent.

4

Monitor the agent

To check the health of the running agent, use the following command:

php artisan nightwatch:status

The nightwatch:status command will exit with a non-zero status code if there is an error. This command will ensure the agent is able to accept connections and send data to Nightwatch.

Use Cloud and Nightwatch Logs

To send your logs to both Laravel Cloud Logs and Nightwatch, use Laravel’s log stacks feature. Update your .env file as shown below to enable logging to both services:

LOG_CHANNEL=stack
LOG_STACK=laravel-cloud-socket,nightwatch

Frequently Asked Questions

Can I log to both Nightwatch and Cloud Logs?

Yes, you can use Laravel Cloud Logs simultaneously with Nightwatch Logging by setting a log stack in your .env in your Cloud environment settings.

Do you need a background process running for each worker cluster?

Yes, you should configure a background process for each worker cluster that needs to run the Nightwatch agent.

Can you run the command from the Commands dashboard of Laravel Cloud?

No, the Nightwatch agent must run as a background process to continuously collect and send data. Running it from the Commands dashboard will not keep the agent running.