Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

install laravel-echo and pusher-js as a simple js file without using npm

To Receiving Broadcasts notifications in laravel 5.3, I needed to include laravel-echo and pusher-js files into my blade templates.

I know that it is possible via npm package manager as has been explained in official laravel docs like this :

npm install --save laravel-echo pusher-js 

And then in resouces/assets/js/app.js file import it like this :

import Echo from "laravel-echo"

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'your-pusher-key-here'
});

But I have a simple js file that all my scripts are Inside it and I did not use npm in my project and all my javascript files are as .js.

I went to laravel-echo github page But I did not find any js formatted file that I can use in my pages.

Is there any way other than npm to include laravel-echo?

Update:

I found that above codes are based on Typescript.I'm not familiar with Typescript. only I know that it can compile to pure javascript files. I am looking for laravel-echo as a js file that I could include it to my page and use included functions and methods. is it posibble?

like image 793
A.B.Developer Avatar asked Sep 10 '16 11:09

A.B.Developer


People also ask

Does laravel require NPM?

By default, Laravel uses NPM to install both of these frontend packages.

Is laravel echo free?

laravel-echo - Libraries - cdnjs - The #1 free and open source CDN built to make life easier for developers.

What is echo in laravel?

Laravel Echo is a JavaScript library that makes it painless to subscribe to channels and listen for events broadcast by your server-side broadcasting driver. You may install Echo via the NPM package manager. In this example, we will also install the pusher-js package.

What is pusher in laravel?

Pusher Channels acts as a realtime layer between your client and server events, maintaining persistent connections to the clients.


1 Answers

I don't know if it's actually possible, but I had the same question as you. I was wirthing my .js files into the public folder. When I tried to add echo, everything went wrong. So, this is my solution, but you will still need to use npm.

  1. I took my .js file and move it to resources\assets\js
  2. Open resources\assets\js\app.js and add require(./filename.js);
  3. run npm install
  4. run npm run dev
  5. Change the link to the new app.js file into my layout.

That way I managed to keep one js file into my code and use laravel-echo.

Maybe there is another solution, but I did not tried it.

  1. Start a new laravel project
  2. run npm install
  3. Modify webpack.mix.js the following way:

mix.js('resources/assets/js/app.js', 'public/js').extract(['echo']);

  1. copy the public/js/vendor.js file to your actual project.

Theorically, this is all the script echo need to be functional. At this point, I think you will need to modify/delete some lines to make it work without the webpack initialisation.

like image 188
Elie Morin Avatar answered Oct 05 '22 22:10

Elie Morin