Package detail

@ably/laravel-echo

ably-forks25.3kMIT1.0.6

Laravel Echo library for beautiful Ably integration

laravel, ably

readme

Introduction

This repository is a fork of https://github.com/laravel/echo. It adheres to public interface methods from base repository. It will be synced regularly with the base repository to make sure all the code is up to date. Ably-specific implementation is added to support native ably-js.

Installation

Install @ably/laravel-echo (wrapper for pluggable lib) and latest version of ably (pluggable lib) using npm.

npm install @ably/laravel-echo ably@1.x

Once Echo is installed, you are ready to create a fresh Echo instance in your application's JavaScript. A great place to do this is at the bottom of the resources/js/bootstrap.js file that is included with the Laravel framework. By default, an example Echo configuration is already included in this file; however, the default configuration in the bootstrap.js file is intended for Pusher. You may copy the configuration below to transition your configuration to Ably.

import Echo from '@ably/laravel-echo';
import * as Ably from 'ably';

window.Ably = Ably; // make globally accessible to Echo
window.Echo = new Echo({
    broadcaster: 'ably',
});

window.Echo.connector.ably.connection.on(stateChange => {
    if (stateChange.current === 'connected') {
        console.log('connected to ably server');
    }
});
  • Take a look at laravel broadcasting auth-endpoint doc for customizing authEndpoint.

      broadcaster: 'ably',
      authEndpoint: '/broadcasting/auth' // relative or absolute url to laravel-server
  • You can set additional ably-js js-clientOptions when creating an Echo instance. Some of those are =>

    broadcaster: 'ably',
    // ably specific client-options
    echoMessages: true, // self-echo for published message is set to false internally.
    queueMessages: true, // default: true, maintains queue for messages to be sent.
    disconnectedRetryTimeout: 15000, // reconnect after 15 seconds when client goes disconnected state
    suspendedRetryTimeout: 30000, // reconnect after 30 seconds when client goes suspended state
  • Auth specific clientOptions should not be used, since laravel authEndpoint is already responsible for token authentication and external auth mechanism is not needed.

Once you have uncommented and adjusted the Echo configuration according to your needs, you may compile your application's assets:

npm run dev
  • If you are using SPA on client side, on user login, new clientId is assigned to the token. So, connection goes into failed state for next channel attach. We have fixed this issue. Now, it immediately reconnects again, reattaching all existing channels. See Fix clientId mismatch for more details. On user logout, connection+channels goes into failed state, and doesn't reconnect again. Hence, you need to create a new Echo instance after user logs out.

Working with laravel sanctum/ support channel auth using custom implementation

  • There is an explicit section for Authorizing Private/Presence Broadcast Channels.
  • Equivalent of this is to provide requestTokenFn as an argument while creating an Echo instance
      echo = new Echo({
              broadcaster: 'ably',
              useTls: true,
              requestTokenFn: async (channelName: string, existingToken: string) => {
                  let postData = { channel_name: channelName, token: existingToken };
                  const res = await axios.post("/api/broadcasting/auth", postData);
                  return res.data;
              },
      });
  • Note: Do not add try/catch statement for above code, since exceptions are being handled internally.

Observing channel messages on ably dev-console and Leaving the channel

  • You can get the internal channel name using ``` //public channel let echoPublicChannel = Echo.channel('channel1'); let ablyPublicChannelName = echoPublicChannel.name; console.log(ablyPublicChannelName); // public:channel1

// private channel let echoPrivateChannel = Echo.private('channel2'); let ablyPrivateChannelName = echoPrivateChannel.name; console.log(ablyPrivateChannelName); // private:channel2

// presence channel let echoPresenceChannel = Echo.join('channel3'); let ablyPresenceChannelName = echoPresenceChannel.name; console.log(ablyPresenceChannelName); // presence:channel3

- Use the same channel name for [observing channel messages on ably dev console](https://ably.com/docs/tools#developer-console).
- Use the same channel name for leaving the channel

Echo.leaveChannel(echoPublicChannel.name); Echo.leaveChannel(echoPrivateChannel.name); Echo.leaveChannel(echoPresenceChannel.name);


## Success/failure confirmation for whisper
- You can get acknowledgement for a whisper/client-event sent on a private/presence channel by passing an extra callback argument.
- Extra callback is a function that receives `err` object representing success/failure.
    echoPrivateChannel.subscribed(() => {
            echoPrivateChannel.whisper('msg', 'Hello there jonny!', (err) => {
                if(err) {
                    console.log('whisper failed with error ' + err);
                } else {
                    console.log('whisper succeeded');
                }
            });
        });

```

Official Documentation

Contributing

  • Make sure all of the public interfacing methods on Echo and Channel object are kept intact irrespective of internal implementation.
  • Follow the below steps for modifying the code.
  • Fork it.
  • Create your feature branch (git checkout -b my-new-feature).
  • Commit your changes (git commit -am 'Add some feature').
  • Ensure you have added suitable tests and the test suite is passing (run vendor/bin/phpunit)
  • Push to the branch (git push origin my-new-feature).
  • Create a new Pull Request.

Release Process

This library uses semantic versioning. For each release, the following needs to be done:

  1. Create a new branch for the release, named like release/1.0.3 (where 1.0.3 is what you're releasing, being the new version).
  2. Update the LIB_VERSION in src/connector/ably-connector.ts.
  3. Update version in pakckage.json.
  4. Run github_changelog_generator to automate the update of the CHANGELOG-ABLY.md. This may require some manual intervention, both in terms of how the command is run and how the change log file is modified. Your mileage may vary:
    • The command you will need to run will look something like this: github_changelog_generator -u ably-forks -p laravel-echo --since-tag ably-echo-1.0.3 --output delta.md --token $GITHUB_TOKEN_WITH_REPO_ACCESS. Generate token here.
    • Using the command above, --output delta.md writes changes made after --since-tag to a new file.
    • The contents of that new file (delta.md) then need to be manually inserted at the top of the CHANGELOG-ABLY.md, changing the "Unreleased" heading and linking with the current version numbers.
    • Also ensure that the "Full Changelog" link points to the new version tag instead of the HEAD.
  5. Commit generated CHANGELOG-ABLY.md file at root.
  6. Make a PR against main.
  7. Once the PR is approved, merge it into main.
  8. Add a tag and push it to origin - e.g.: git tag ably-echo-1.0.3 && git push origin ably-echo-1.0.3.
  9. Publish npm package on npmjs.com.
  10. Visit https://github.com/ably-forks/laravel-echo/tags and add release notes to the release (generally you can just copy the notes you added to the CHANGELOG).
  11. Update the Ably Changelog (via headwayapp) with these changes (again, you can just copy the notes you added to the CHANGELOG).

Note

  • Current README is newly created and located under .github/README.md.
  • CHANGELOG-ABLY.md will be used for commiting changelog instead of CHANGELOG.md.
  • This is mainly to avoid syncing conflicts with base repository.

changelog

Release Notes

Unreleased

v1.16.1 - 2024-04-09

v1.16.0 - 2024-02-20

v1.15.3 - 2023-08-29

v1.15.2 - 2023-07-11

v1.15.1 - 2023-04-26

v1.15.0 - 2023-01-17

Added

v1.14.2 - 2022-11-22

Fixed

v1.14.1 - 2022-10-25

Fixed

v1.14.0 - 2022-08-30

Added

v1.13.1 - 2022-08-02

Changed

v1.13.0 - 2022-07-26

Added

v1.12.1 - 2022-07-13

Fixed

v1.12.0 - 2022-06-21

Added

v1.11.7 - 2022-04-21

No significant changes.

v1.11.6 - 2022-04-21

No significant changes.

v1.11.5 - 2022-04-12

Changed

v1.11.4 - 2022-03-15

Changed

v1.11.3 (2021-10-26)

Fixed

  • Fix types in connector.ts (#327)

v1.11.2 (2021-08-31)

Changed

  • Package build

v1.11.1 (2021-08-03)

Changed

  • Extend presence channel by channel class (#318)

v1.11.0 (2021-06-17)

Added

  • Add listenToAll and stopListeningToAll (#315)

v1.10.0 (2020-12-19)

Added

  • Add optional callback argument to stopListening() (#292)

v1.9.0 (2020-10-13)

Added

  • Register subscription succeeded callbacks (#288)

v1.8.1 (2020-07-31)

Added

  • Implement error handling with support for Pusher (#284)

v1.8.0 (2020-05-16)

Fixed

  • IE11 fix for dist/echo.js not being transpiled to ES5 (#270)

v1.7.0 (2020-04-03)

Added

  • Add pusher private-encrypted (#264)

v1.6.1 (2019-10-01)

Fixed

  • Change check for 'querySelector' method for browser compatibility (#253)

v1.6.0 (2019-09-24)

Added

  • Add stopWhisper method to channel (#243)
  • Add option this.options.withoutInterceptors (#248)
  • Add support for custom connectors (#247)

Fixed

  • Check for querySelector (#251)

v1.5.4 (2019-06-14)

Fixed

  • Ensure passed param wins over global (#235)

v1.5.3 (2019-02-14)

Fixed

  • Add reference to TS declaration file (#222)
  • Add missing method to Echo instance (fd3b65b)

v1.5.2 (2018-12-12)

Added

  • Add iife output to dist (#214)
  • Add leaveOne method to connectors (#216, 9809405)

v1.5.1 (2018-12-05)

Added

  • Add commonjs output to distribution (#212)

v1.5.0 (2018-12-01)

Changed

  • General maintenance, code styling, and add stopListening() for socket.io (#210)