├── .editorconfig ├── .eslintignore ├── .eslintrc ├── .github └── workflows │ └── ci.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── jest.config.js ├── package.json ├── renovate.json ├── src ├── module.ts └── runtime │ ├── echo.ts │ ├── index.ts │ ├── options.js │ ├── plugin.js │ └── types.ts ├── test ├── basic.test.ts ├── custom-options-path.test.ts ├── fixture │ ├── basic │ │ ├── nuxt.config.ts │ │ ├── pages │ │ │ └── index.vue │ │ └── tsconfig.json │ ├── custom-options-path │ │ ├── laravel-echo.ts │ │ ├── nuxt.config.ts │ │ ├── pages │ │ │ └── index.vue │ │ └── tsconfig.json │ ├── options-path │ │ ├── app │ │ │ └── laravel-echo │ │ │ │ └── options.js │ │ ├── nuxt.config.ts │ │ ├── pages │ │ │ └── index.vue │ │ └── tsconfig.json │ └── with-plugins │ │ ├── nuxt.config.ts │ │ ├── pages │ │ └── index.vue │ │ ├── plugins │ │ └── echo.js │ │ └── tsconfig.json ├── options-path.test.ts └── with-plugins.test.ts ├── tsconfig.json └── yarn.lock /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_size = 2 6 | indent_style = space 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | # Common 2 | node_modules 3 | dist 4 | .nuxt 5 | coverage 6 | 7 | # Plugin 8 | src/runtime/*.js 9 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "@nuxtjs/eslint-config-typescript" 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: ci 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | branches: 9 | - main 10 | 11 | jobs: 12 | ci: 13 | runs-on: ${{ matrix.os }} 14 | 15 | strategy: 16 | matrix: 17 | # os: [ubuntu-latest, macos-latest, windows-latest] 18 | os: [ubuntu-latest] 19 | node: [12] 20 | 21 | steps: 22 | - uses: actions/setup-node@v2 23 | with: 24 | node-version: ${{ matrix.node }} 25 | 26 | - name: checkout 27 | uses: actions/checkout@v2 28 | 29 | - name: cache node_modules 30 | uses: actions/cache@v2 31 | with: 32 | path: node_modules 33 | key: ${{ matrix.os }}-node-v${{ matrix.node }}-deps-${{ hashFiles(format('{0}{1}', github.workspace, '/yarn.lock')) }} 34 | 35 | - name: Install dependencies 36 | if: steps.cache.outputs.cache-hit != 'true' 37 | run: yarn 38 | 39 | - name: Lint 40 | run: yarn lint 41 | 42 | - name: Test 43 | run: yarn test 44 | 45 | - name: Coverage 46 | uses: codecov/codecov-action@v1 47 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.iml 3 | .idea 4 | *.log* 5 | .nuxt 6 | .vscode 7 | .DS_Store 8 | coverage 9 | dist 10 | sw.* 11 | .env 12 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | 5 | ## [2.0.0-alpha.5](https://github.com/nuxt-community/laravel-echo-module/compare/v2.0.0-alpha.4...v2.0.0-alpha.5) (2021-04-07) 6 | 7 | 8 | ### Bug Fixes 9 | 10 | * export types ([9bdd3ef](https://github.com/nuxt-community/laravel-echo-module/commit/9bdd3effae5187397296d524214a62ea2bc9f826)) 11 | 12 | ## [2.0.0-alpha.4](https://github.com/nuxt-community/laravel-echo-module/compare/v2.0.0-alpha.3...v2.0.0-alpha.4) (2021-04-07) 13 | 14 | ## [2.0.0-alpha.3](https://github.com/nuxt-community/laravel-echo-module/compare/v2.0.0-alpha.2...v2.0.0-alpha.3) (2021-04-07) 15 | 16 | 17 | ### Bug Fixes 18 | 19 | * hooks ([b5a1e07](https://github.com/nuxt-community/laravel-echo-module/commit/b5a1e07e7d73831fd0e7bd616b228ddfa36e9a5b)) 20 | * options ([96a5060](https://github.com/nuxt-community/laravel-echo-module/commit/96a5060c4f091e0e48454710dd4a645b6de63177)) 21 | 22 | ## [2.0.0-alpha.2](https://github.com/nuxt-community/laravel-echo-module/compare/v2.0.0-alpha.1...v2.0.0-alpha.2) (2021-04-05) 23 | 24 | 25 | ### Features 26 | 27 | * add hooks before/after connect/disconnect ([87b5dfa](https://github.com/nuxt-community/laravel-echo-module/commit/87b5dfa2d2876cad09991b43eaed136c69b005f4)) 28 | 29 | 30 | ### Bug Fixes 31 | 32 | * disable plugin on ssr ([51b8d2e](https://github.com/nuxt-community/laravel-echo-module/commit/51b8d2e6737004c4644704bdd99d875b8dfd7282)) 33 | 34 | ## [2.0.0-alpha.1](https://github.com/nuxt-community/laravel-echo-module/compare/v2.0.0-alpha.0...v2.0.0-alpha.1) (2021-03-31) 35 | 36 | 37 | ### Features 38 | 39 | * rewrite typescript ([d826db0](https://github.com/nuxt-community/laravel-echo-module/commit/d826db0572c2cea499a0ce595f65c692a74e7971)) 40 | * support runtime config ([3495fb8](https://github.com/nuxt-community/laravel-echo-module/commit/3495fb86cb3b6394a7348e06ac044d3f9e7e072a)) 41 | 42 | ## [2.0.0-alpha.0](https://github.com/nuxt-community/laravel-echo/compare/v1.1.0...v2.0.0-alpha.0) (2021-03-29) 43 | 44 | 45 | ### Features 46 | 47 | * miragte from auth module v4 to v5 ([#14](https://github.com/nuxt-community/laravel-echo/issues/14)) ([31a1098](https://github.com/nuxt-community/laravel-echo/commit/31a10987dde1f94d3a11ff67da0b35a97481130f)) 48 | 49 | ## [1.1.0](https://github.com/nuxt-community/laravel-echo/compare/v1.0.3...v1.1.0) (2020-01-27) 50 | 51 | 52 | ### Features 53 | 54 | * add plugins option ([#8](https://github.com/nuxt-community/laravel-echo/issues/8)) ([da90251](https://github.com/nuxt-community/laravel-echo/commit/da90251)) 55 | 56 | ### [1.0.3](https://github.com/nuxt-community/laravel-echo/compare/v1.0.2...v1.0.3) (2019-12-11) 57 | 58 | 59 | ### Bug Fixes 60 | 61 | * set default `broadcaster` to `null` ([f8d950e](https://github.com/nuxt-community/laravel-echo/commit/f8d950e)) 62 | 63 | ### [1.0.2](https://github.com/nuxt-community/laravel-echo/compare/v1.0.1...v1.0.2) (2019-10-23) 64 | 65 | 66 | ### Bug Fixes 67 | 68 | * ssr ([#4](https://github.com/nuxt-community/laravel-echo/issues/4)) ([264d1f8](https://github.com/nuxt-community/laravel-echo/commit/264d1f8)) 69 | 70 | ### [1.0.1](https://github.com/nuxt-community/laravel-echo/compare/v1.0.0...v1.0.1) (2019-10-17) 71 | 72 | 73 | ### Bug Fixes 74 | 75 | * register plugin on hook `builder:extendPlugins` ([ba3fe9e](https://github.com/nuxt-community/laravel-echo/commit/ba3fe9e)) 76 | 77 | ## 1.0.0 (2019-09-25) 78 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Nuxt Community 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # @nuxtjs/laravel-echo 2 | 3 | [![npm version][npm-version-src]][npm-version-href] 4 | [![npm downloads][npm-downloads-src]][npm-downloads-href] 5 | [![Github Actions CI][github-actions-ci-src]][github-actions-ci-href] 6 | [![Codecov][codecov-src]][codecov-href] 7 | [![License][license-src]][license-href] 8 | 9 | > Laravel Echo for Nuxt 2 10 | 11 | [📖 **Release Notes**](./CHANGELOG.md) 12 | 13 | ## Requirements 14 | 15 | If you use the broadcaster `pusher`, you need to ensure that you have `pusher-js` installed: 16 | 17 | ```bash 18 | yarn add pusher-js # or npm install pusher-js 19 | ``` 20 | 21 | If you use the broadcaster `socket.io`, you need to ensure that you have `socket.io-client` installed: 22 | 23 | ```bash 24 | yarn add socket.io-client # or npm install socket.io-client 25 | ``` 26 | 27 | ## Setup 28 | 29 | 1. Add `@nuxtjs/laravel-echo` dependency to your project 30 | 31 | ```bash 32 | yarn add --dev @nuxtjs/laravel-echo # or npm install --save-dev @nuxtjs/laravel-echo 33 | ``` 34 | 35 | 2. Add `@nuxtjs/laravel-echo` to the `buildModules` section of `nuxt.config.js` 36 | 37 | ```js 38 | export default { 39 | buildModules: [ 40 | // Simple usage 41 | '@nuxtjs/laravel-echo', 42 | 43 | // With options 44 | ['@nuxtjs/laravel-echo', { /* module options */ }] 45 | ] 46 | } 47 | ``` 48 | 49 | :warning: If you are using Nuxt **< v2.9** you have to install the module as a `dependency` (No `--dev` or `--save-dev` flags) and use `modules` section in `nuxt.config.js` instead of `buildModules`. 50 | 51 | ### Using top level options 52 | 53 | ```js 54 | export default { 55 | buildModules: [ 56 | '@nuxtjs/laravel-echo' 57 | ], 58 | echo: { 59 | /* module options */ 60 | } 61 | } 62 | ``` 63 | 64 | ## Options 65 | 66 | ### `broadcaster` 67 | 68 | - Type: `String` 69 | - Default: `'null'` 70 | 71 | You can use `'pusher'`, `'socket.io'` or `'null'`. 72 | 73 | See [https://laravel.com/docs/broadcasting#driver-prerequisites](https://laravel.com/docs/broadcasting#driver-prerequisites) 74 | 75 | ### `plugins` 76 | 77 | - Type: `Array` 78 | - Default: `null` 79 | 80 | If you have plugins that need to access `$echo`, you can use `echo.plugins` option. 81 | 82 | > **Note:** Plugins are pushed in client mode only (`ssr: false`). 83 | 84 | `nuxt.config.js` 85 | 86 | ```js 87 | export default { 88 | buildModules: [ 89 | '@nuxtjs/laravel-echo' 90 | ], 91 | echo: { 92 | plugins: [ '~/plugins/echo.js' ] 93 | } 94 | } 95 | ``` 96 | 97 | `plugins/echo.js` 98 | 99 | ```js 100 | export default function ({ $echo }) { 101 | // Echo is available here 102 | console.log($echo) 103 | } 104 | ``` 105 | 106 | ### `authModule` 107 | 108 | - Type: `Boolean` 109 | - Default: `false` 110 | 111 | Integration with [Auth Module](https://github.com/nuxt-community/auth-module). 112 | 113 | ### `connectOnLogin` 114 | 115 | - Type: `Boolean` 116 | - Default: `false` 117 | 118 | Connect the connector on login, if `authModule` is set `true`. 119 | 120 | ### `disconnectOnLogout` 121 | 122 | - Type: `Boolean` 123 | - Default: `false` 124 | 125 | Disconnect the connector on logout, if `authModule` is set `true`. 126 | 127 | ## Usage 128 | 129 | This module inject `$echo` to your project: 130 | 131 | ```html 132 | 137 | 138 | 148 | ``` 149 | 150 | ## License 151 | 152 | [MIT License](./LICENSE) 153 | 154 | Copyright (c) Nuxt Community 155 | 156 | 157 | [npm-version-src]: https://img.shields.io/npm/v/@nuxtjs/laravel-echo/latest.svg 158 | [npm-version-href]: https://npmjs.com/package/@nuxtjs/laravel-echo 159 | 160 | [npm-downloads-src]: https://img.shields.io/npm/dt/@nuxtjs/laravel-echo.svg 161 | [npm-downloads-href]: https://npmjs.com/package/@nuxtjs/laravel-echo 162 | 163 | [github-actions-ci-src]: https://github.com/nuxt-community/laravel-echo-module/workflows/ci/badge.svg 164 | [github-actions-ci-href]: https://github.com/nuxt-community/laravel-echo-module/actions?query=workflow%3Aci 165 | 166 | [codecov-src]: https://img.shields.io/codecov/c/github/nuxt-community/laravel-echo-module.svg 167 | [codecov-href]: https://codecov.io/gh/nuxt-community/laravel-echo-module 168 | 169 | [license-src]: https://img.shields.io/npm/l/@nuxtjs/laravel-echo.svg 170 | [license-href]: https://npmjs.com/package/@nuxtjs/laravel-echo 171 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | preset: '@nuxt/test-utils', 3 | collectCoverageFrom: ['src/**', '!src/runtime/**'] 4 | } 5 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@nuxtjs/laravel-echo", 3 | "version": "2.0.0-alpha.5", 4 | "description": "Laravel Echo for Nuxt.js", 5 | "keywords": [ 6 | "nuxt", 7 | "module", 8 | "nuxt-module", 9 | "laravel-echo", 10 | "echo", 11 | "laravel" 12 | ], 13 | "repository": "nuxt-community/laravel-echo-module", 14 | "license": "MIT", 15 | "contributors": [ 16 | "Ricardo Gobbo de Souza " 17 | ], 18 | "exports": { 19 | ".": "./dist/module.js", 20 | "./dist/runtime/*": "./dist/runtime/*", 21 | "./package.json": "./package.json" 22 | }, 23 | "main": "./dist/module.js", 24 | "types": "./dist/runtime.d.ts", 25 | "files": [ 26 | "dist" 27 | ], 28 | "scripts": { 29 | "build": "siroc build", 30 | "lint": "eslint --ext .js,.ts,.vue .", 31 | "release": "yarn build && yarn lint && yarn test && standard-version && git push --follow-tags && npm publish", 32 | "test": "jest" 33 | }, 34 | "dependencies": { 35 | "defu": "^3.2.2", 36 | "laravel-echo": "^1.10.0" 37 | }, 38 | "devDependencies": { 39 | "@nuxt/test-utils": "latest", 40 | "@nuxt/types": "latest", 41 | "@nuxt/typescript-build": "latest", 42 | "@nuxtjs/eslint-config-typescript": "latest", 43 | "@types/jest": "latest", 44 | "@types/node": "latest", 45 | "eslint": "latest", 46 | "jest": "latest", 47 | "nuxt": "latest", 48 | "siroc": "latest", 49 | "standard-version": "latest" 50 | }, 51 | "publishConfig": { 52 | "access": "public" 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "@nuxtjs" 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /src/module.ts: -------------------------------------------------------------------------------- 1 | import { resolve, join } from 'path' 2 | import { existsSync } from 'fs' 3 | import defu from 'defu' 4 | import { Module } from '@nuxt/types' 5 | import { NuxtOptionsPlugin } from '@nuxt/types/config/plugin' 6 | import { name, version } from '../package.json' 7 | import { ModuleOptions } from './runtime/types' 8 | 9 | const DEFAULTS: ModuleOptions = { 10 | broadcaster: 'null', 11 | encrypted: false, 12 | authModule: false, 13 | connectOnLogin: true, 14 | disconnectOnLogout: true 15 | } 16 | 17 | const nuxtModule: Module = function (moduleOptions) { 18 | this.nuxt.hook('builder:extendPlugins', (plugins: NuxtOptionsPlugin[]) => { 19 | const options = defu( 20 | this.options.echo || {}, 21 | moduleOptions, 22 | DEFAULTS 23 | ) 24 | 25 | const runtimeDir = resolve(__dirname, 'runtime') 26 | this.options.alias['~echo'] = runtimeDir 27 | this.options.build.transpile.push(runtimeDir, 'laravel-echo', 'defu') 28 | 29 | const optionsPath: string = this.nuxt.resolver.resolveAlias(options.optionsPath || 30 | join(this.options.dir!.app || 'app', 'laravel-echo', 'options.js')) 31 | 32 | delete options.optionsPath 33 | 34 | // Register options template 35 | this.addTemplate({ 36 | fileName: `laravel-echo/options.${optionsPath && optionsPath.endsWith('ts') ? 'ts' : 'js'}`, 37 | src: existsSync(optionsPath) ? optionsPath : resolve(__dirname, './runtime/options.js'), 38 | options 39 | }) 40 | 41 | // Copy echo plugin 42 | const { dst } = this.addTemplate({ 43 | src: resolve(__dirname, './runtime/plugin.js'), 44 | fileName: 'laravel-echo/plugin.js', 45 | options 46 | }) 47 | 48 | plugins.push({ 49 | src: resolve(this.options.buildDir, dst), 50 | ssr: false 51 | }) 52 | 53 | // Extend echo with plugins 54 | if (options.plugins) { 55 | options.plugins.forEach(p => plugins.push({ src: p, ssr: false })) 56 | 57 | delete options.plugins 58 | } 59 | }) 60 | } 61 | 62 | ;(nuxtModule as any).meta = { name, version } 63 | 64 | export default nuxtModule 65 | -------------------------------------------------------------------------------- /src/runtime/echo.ts: -------------------------------------------------------------------------------- 1 | import BaseEcho from 'laravel-echo' 2 | import defu from 'defu' 3 | import { Context } from '@nuxt/types' 4 | import { ModuleOptions } from './types' 5 | 6 | export class Echo extends BaseEcho { 7 | ctx: Context; 8 | 9 | constructor (ctx: Context, options: Partial = {}) { 10 | super(defu((ctx.$config || {}).echo || {}, options)) 11 | 12 | this.ctx = ctx 13 | this.options.auth = this.options.auth || {} 14 | } 15 | 16 | async init () { 17 | this.options.auth.headers = await this.getHeaders() 18 | this.watchAuthState() 19 | } 20 | 21 | async getHeaders () { 22 | let headers: any = {} 23 | 24 | if (typeof headers === 'function') { 25 | headers = await headers(this.ctx) 26 | } 27 | 28 | if (this.options.authModule && this.ctx.app.$auth) { 29 | const strategy = this.ctx.app.$auth.strategy 30 | 31 | if (strategy.options.name === 'laravelSanctum') { 32 | headers.referer = location.origin 33 | headers['X-XSRF-TOKEN'] = this.ctx.app.$auth.$storage.getCookies()['XSRF-TOKEN'] 34 | } else { 35 | const tokenName = strategy.options.token.name || 'Authorization' 36 | const token = strategy.token.get() 37 | 38 | if (token) { 39 | headers[tokenName] = token 40 | } 41 | } 42 | } 43 | 44 | return defu(headers, this.options.auth.headers) 45 | } 46 | 47 | async connect () { 48 | if (this.options.onBeforeConnect) { 49 | await this.options.onBeforeConnect().bind(this) 50 | } 51 | 52 | super.connect() 53 | 54 | if (this.options.onAfterConnect) { 55 | await this.options.onAfterConnect().bind(this) 56 | } 57 | } 58 | 59 | async disconnect () { 60 | if (this.options.onBeforeDisconnect) { 61 | await this.options.onBeforeDisconnect().bind(this) 62 | } 63 | 64 | super.disconnect() 65 | 66 | if (this.options.onAfterDisconnect) { 67 | await this.options.onAfterDisconnect().bind(this) 68 | } 69 | } 70 | 71 | watchAuthState () { 72 | if (this.options.authModule && this.ctx.app.$auth) { 73 | this.ctx.app.$auth.$storage.watchState('loggedIn', async (loggedIn: boolean) => { 74 | this.options.auth.headers = await this.getHeaders() 75 | 76 | if (this.options.connectOnLogin && loggedIn) { 77 | await this.connect() 78 | } 79 | 80 | if (this.options.disconnectOnLogout && !loggedIn && this.connector) { 81 | await this.disconnect() 82 | } 83 | }).bind(this) 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/runtime/index.ts: -------------------------------------------------------------------------------- 1 | import { Echo } from './echo' 2 | import { ModuleOptions } from './types' 3 | 4 | declare module '@nuxt/types' { 5 | interface NuxtAppOptions { 6 | $echo: Echo; 7 | } 8 | 9 | interface Context { 10 | $echo: Echo; 11 | } 12 | 13 | // Nuxt 2.14+ 14 | interface NuxtConfig { 15 | echo?: Partial 16 | } 17 | 18 | // Nuxt 2.9 - 2.13 19 | interface Configuration { 20 | echo?: Partial 21 | } 22 | } 23 | 24 | declare module 'vue/types/vue' { 25 | interface Vue { 26 | $echo: Echo 27 | } 28 | } 29 | 30 | declare module 'vuex/types/index' { 31 | // eslint-disable-next-line no-unused-vars,@typescript-eslint/no-unused-vars 32 | interface Store { 33 | $echo: Echo 34 | } 35 | } 36 | 37 | export * from './echo' 38 | -------------------------------------------------------------------------------- /src/runtime/options.js: -------------------------------------------------------------------------------- 1 | export default <%= serializeFunction(options) %> 2 | -------------------------------------------------------------------------------- /src/runtime/plugin.js: -------------------------------------------------------------------------------- 1 | import defu from 'defu' 2 | import options from './options' 3 | import { Echo } from '~echo' 4 | 5 | /** 6 | * @type {import('@nuxt/types').Plugin} 7 | */ 8 | export default async function (ctx, inject) { 9 | const echoOptions = typeof options === 'function' ? await options(ctx) : options 10 | 11 | <% if (options.broadcaster === 'pusher' && !options.encrypted) { %> 12 | if (!window.Pusher) window.Pusher = require('pusher-js') 13 | <% } %> 14 | 15 | <% if (options.broadcaster === 'pusher' && options.encrypted) { %> 16 | if (!window.Pusher) window.Pusher = require('pusher-js/with-encryption') 17 | <% } %> 18 | 19 | <% if (options.broadcaster === 'socket.io') { %> 20 | if (!window.io) window.io = require('socket.io-client') 21 | <% } %> 22 | 23 | const echo = new Echo(ctx, defu(echoOptions, <%= serializeFunction(options) %>)) 24 | await echo.init() 25 | 26 | inject('echo', echo) 27 | ctx.$echo = echo 28 | } 29 | -------------------------------------------------------------------------------- /src/runtime/types.ts: -------------------------------------------------------------------------------- 1 | export type EchoOptions = Record 2 | 3 | export interface ModuleOptions extends EchoOptions { 4 | broadcaster?: string, 5 | encrypted?: boolean, 6 | plugins?: string[], 7 | authModule?: boolean, 8 | connectOnLogin?: boolean, 9 | disconnectOnLogout?: boolean, 10 | optionsPath?: string, 11 | onBeforeConnect?: Function, 12 | onAfterConnect?: Function, 13 | onBeforeDisconnect?: Function, 14 | onAfterDisconnect?: Function, 15 | } 16 | -------------------------------------------------------------------------------- /test/basic.test.ts: -------------------------------------------------------------------------------- 1 | import { setupTest, getNuxt, get, url } from '@nuxt/test-utils' 2 | 3 | describe('basic', () => { 4 | setupTest({ 5 | server: true, 6 | fixture: 'fixture/basic' 7 | }) 8 | 9 | test('echo should be defined', async () => { 10 | const window = await getNuxt().renderAndGetWindow(url('/')) 11 | expect(window.$nuxt.$echo).toBeDefined() 12 | }) 13 | 14 | test('render', async () => { 15 | const { body } = await get('/') 16 | expect(body).toContain('Works!') 17 | }) 18 | }) 19 | -------------------------------------------------------------------------------- /test/custom-options-path.test.ts: -------------------------------------------------------------------------------- 1 | import { setupTest, getNuxt, get, url } from '@nuxt/test-utils' 2 | 3 | describe('custom-options-path', () => { 4 | setupTest({ 5 | server: true, 6 | fixture: 'fixture/custom-options-path' 7 | }) 8 | 9 | test('onAfterConnect should be defined', async () => { 10 | const window = await getNuxt().renderAndGetWindow(url('/')) 11 | expect(window.$nuxt.$echo.options.onAfterConnect).toBeDefined() 12 | }) 13 | 14 | test('render', async () => { 15 | const { body } = await get('/') 16 | expect(body).toContain('Works!') 17 | }) 18 | }) 19 | -------------------------------------------------------------------------------- /test/fixture/basic/nuxt.config.ts: -------------------------------------------------------------------------------- 1 | 2 | import { NuxtConfig } from '@nuxt/types' 3 | 4 | export default { 5 | buildModules: [ 6 | '@nuxt/typescript-build', 7 | '../../../src/module.ts' 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /test/fixture/basic/pages/index.vue: -------------------------------------------------------------------------------- 1 | 6 | -------------------------------------------------------------------------------- /test/fixture/basic/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../../../tsconfig.json" 3 | } 4 | -------------------------------------------------------------------------------- /test/fixture/custom-options-path/laravel-echo.ts: -------------------------------------------------------------------------------- 1 | export default { 2 | onAfterConnect () { 3 | // 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /test/fixture/custom-options-path/nuxt.config.ts: -------------------------------------------------------------------------------- 1 | 2 | import { NuxtConfig } from '@nuxt/types' 3 | 4 | export default { 5 | buildModules: [ 6 | '@nuxt/typescript-build', 7 | '../../../src/module.ts' 8 | ], 9 | 10 | echo: { 11 | optionsPath: './laravel-echo.ts' 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /test/fixture/custom-options-path/pages/index.vue: -------------------------------------------------------------------------------- 1 | 6 | -------------------------------------------------------------------------------- /test/fixture/custom-options-path/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../../../tsconfig.json" 3 | } 4 | -------------------------------------------------------------------------------- /test/fixture/options-path/app/laravel-echo/options.js: -------------------------------------------------------------------------------- 1 | export default { 2 | onBeforeConnect () { 3 | // 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /test/fixture/options-path/nuxt.config.ts: -------------------------------------------------------------------------------- 1 | 2 | import { NuxtConfig } from '@nuxt/types' 3 | 4 | export default { 5 | buildModules: [ 6 | '@nuxt/typescript-build', 7 | '../../../src/module.ts' 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /test/fixture/options-path/pages/index.vue: -------------------------------------------------------------------------------- 1 | 6 | -------------------------------------------------------------------------------- /test/fixture/options-path/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../../../tsconfig.json" 3 | } 4 | -------------------------------------------------------------------------------- /test/fixture/with-plugins/nuxt.config.ts: -------------------------------------------------------------------------------- 1 | import { NuxtConfig } from '@nuxt/types' 2 | 3 | export default { 4 | buildModules: [ 5 | '@nuxt/typescript-build', 6 | '../../../src/module.ts' 7 | ], 8 | echo: { 9 | plugins: [ 10 | '~/plugins/echo.js' 11 | ] 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /test/fixture/with-plugins/pages/index.vue: -------------------------------------------------------------------------------- 1 | 6 | -------------------------------------------------------------------------------- /test/fixture/with-plugins/plugins/echo.js: -------------------------------------------------------------------------------- 1 | export default function ({ $echo }) { 2 | $echo.plugin = true 3 | } 4 | -------------------------------------------------------------------------------- /test/fixture/with-plugins/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../../../tsconfig.json" 3 | } 4 | -------------------------------------------------------------------------------- /test/options-path.test.ts: -------------------------------------------------------------------------------- 1 | import { setupTest, getNuxt, get, url } from '@nuxt/test-utils' 2 | 3 | describe('options-path', () => { 4 | setupTest({ 5 | server: true, 6 | fixture: 'fixture/options-path' 7 | }) 8 | 9 | test('onBeforeConnect should be defined', async () => { 10 | const window = await getNuxt().renderAndGetWindow(url('/')) 11 | expect(window.$nuxt.$echo.options.onBeforeConnect).toBeDefined() 12 | }) 13 | 14 | test('render', async () => { 15 | const { body } = await get('/') 16 | expect(body).toContain('Works!') 17 | }) 18 | }) 19 | -------------------------------------------------------------------------------- /test/with-plugins.test.ts: -------------------------------------------------------------------------------- 1 | import { setupTest, getNuxt, get, url } from '@nuxt/test-utils' 2 | 3 | describe('with-plugins', () => { 4 | setupTest({ 5 | server: true, 6 | fixture: 'fixture/with-plugins' 7 | }) 8 | 9 | test('echo plugin should be defined', async () => { 10 | const window = await getNuxt().renderAndGetWindow(url('/')) 11 | expect(window.$nuxt.$echo).toBeDefined() 12 | expect(window.$nuxt.$echo.plugin).toBe(true) 13 | }) 14 | 15 | test('render', async () => { 16 | const { body } = await get('/') 17 | expect(body).toContain('Works!') 18 | }) 19 | }) 20 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "ESNext", 4 | "module": "ESNext", 5 | "moduleResolution": "Node", 6 | "resolveJsonModule": true, 7 | "types": [ 8 | "node", 9 | "jest" 10 | ] 11 | }, 12 | "exclude": [ 13 | "node_modules", 14 | "dist" 15 | ] 16 | } 17 | --------------------------------------------------------------------------------