0

I've been trying to diagnose an issue with pusher not working on production, when it works fine locally. This morning I discovered after tinkering around in the console, that when I do this on production:

Echo.private('App.Models.User.1');

I get this output:

enter image description here

which is the key from my development .env file:

PUSHER_APP_KEY=f9d9********011e

My production key shown in pusher is:

key = "07ae********2d4"

My production .env file also references this correct production key:

PUSHER_APP_KEY=07ae*********2d4

HOWEVER, that is NOT what's actually being used by the production application (see prior screenshot).

I'm not doing anything different from the "stock" implementation of importing pusher in my bootstrap.js file:

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,
    encrypted: true
});

After searching my codebase, I found that the key gets hard-coded into the public/js/app.js file when you run npm run dev locally. I verified this by changing the key in my .env file and running npm run dev which then updated the key reflected in the app.js file.

I then went out to my production site's FTP and downloaded the app.js file, and sure enough, the development key is hard-coded in the app.js file.

If I update the app.js file to reflect the correct key, and then run Echo.private('App.Models.User.1'); again, this is the output, which shows the correct key:

enter image description here

And also, notifications start working as expected after making that change. However, that's obviously problematic as every time I deploy, it will be overwritten by the dev value.

Could it be my build process? (also see this SO question I asked yesterday)

What the actual *** is going on here?

I've not had any other issues with npm not building things correctly (that I'm aware of), but it seems that the code in my app.js file must be getting generated via the .env file and somehow my production environment is not referencing the correct key?

Here are some details around my build process (I use Github Actions).

Here is my github actions .yml file:

    steps:
      - name: Set up MySQL
        run: |
          sudo systemctl start mysql
          mysql -e 'CREATE DATABASE testdb;' -uroot -proot
          mysql -e 'SHOW DATABASES;' -uroot -proot
      - uses: actions/checkout@main
      - name: Copy .env
        run: php -r "file_exists('.env') || copy('.env.example', '.env');"
      - name: Install Dependencies
        run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress
      - name: Generate key
        run: php artisan key:generate
      - name: Directory Permissions
        run: chmod -R 777 storage bootstrap/cache
      - name: Clean Install
        run: npm ci
      - name: Compile assets
        run: npm run prod
      - name: Execute tests (Unit and Feature tests) via PHPUnit
        run: vendor/bin/phpunit

from my package.json file:

    "scripts": {
        "dev": "npm run development",
        "development": "mix",
        "watch": "mix watch",
        "watch-poll": "mix watch -- --watch-options-poll=1000",
        "hot": "mix watch --hot",
        "prod": "npm run production",
        "production": "mix --production"
    },

UPDATE:

My public folder, and in turn my public/js/app.js file is getting pushed to source control. I just deleted it from my repo and pushed the code to production, and now I'm getting a jquery not defined error, which tells me that the app.js file isn't getting re-created during my build process.

UPDATE:

My .env file is not in source control, so the github action is using .env.example which has the variables but no values, and has a couple of other "mix" variables, which may be the problem.

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

as mentioned earlier in the question, my bootstrap.js file is referencing those 2 MIX_PUSHER_* variables:

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    encrypted: true
});

I guess I need to set up a github environment variable for the app key for each environment?

UPDATE:

I found this SO answer that details how to create a github secret and basically put your whole environment file in it, but it's saying it's an invalid:

The environment file is invalid!
Failed to parse dotenv file. Encountered unexpected whitespace 
hyphen
  • 2,368
  • 5
  • 28
  • 59
  • Which lib you using for ws? – Maik Lowrey Feb 08 '23 at 13:09
  • I'm using pusher – hyphen Feb 08 '23 at 13:12
  • laravel-websockets or pusher.io? – Maik Lowrey Feb 08 '23 at 13:14
  • how ever. it seems that you need to compile your javascript for production. did you do that? – Maik Lowrey Feb 08 '23 at 13:17
  • i suggest that the problem goes hand by hand with your previous question from yesterday: https://stackoverflow.com/questions/75376297/laravel-echo-working-locally-but-not-once-deployed. Do you was able to fix it? – Maik Lowrey Feb 08 '23 at 13:31
  • I'm running all the appropriate commands as a part of my build script – hyphen Feb 08 '23 at 13:44
  • 1
    Do you have your production `.env` values on the build server? – ceejayoz Feb 08 '23 at 14:15
  • So, I was wondering about that. My .env file is not in source control, so it's using the .env.example file. That file has the pusher variables, but no those variables have no values. Should I set up github environment variables for those? I'm a little confused about the best way to make sure the build process sees the correct value. Adding detail to my question – hyphen Feb 08 '23 at 15:21
  • 1
    @hyphen Yeah, that's your issue. The build server is putting the build server's `.env` vars into the finalized code. You either want to put the production `.env` values on the build server, or do something like (probably in a prettier fashion than this pseudocode) `` in your app's layout and use `window.PUSHER_APP_KEY` in your script. – ceejayoz Feb 08 '23 at 19:53

1 Answers1

0

The problem occurs from your js. It seems that you are using the development env in your js for production. Make sure that you are compile your js files for production in your live environment.

Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
  • 1
    I thought that was fairly obvious from my question. I understand the problem, I don't understand the fix. I'm running what should be the appropriate commands to get a production build, which is happening successfully in my github action. – hyphen Feb 08 '23 at 14:12