@soketi/soketi-js

1.4.3 • Public • Published

Soketi.js

CI Latest Stable Version Total Downloads License

Soketi.js is a hard fork of Laravel Echo that works with Soketi, a Laravel-ready WebSockets service.

🤝 Supporting

Renoki Co. on GitHub aims on bringing a lot of open source projects and helpful projects to the world. Developing and maintaining projects everyday is a harsh work and tho, we love it.

If you are using your application in your day-to-day job, on presentation demos, hobby projects or even school projects, spread some kind words about our work or sponsor our work. Kind words will touch our chakras and vibe, while the sponsorships will keep the open source projects alive.

ko-fi

🚀 Installation

You can install the package via npm:

npm install --save-dev @soketi/soketi-js

🙌 Usage

Soketi.js is a hard fork of laravel/echo, meaning that you can use it as a normal Echo client, being fully compatible with all the docs in the Broadcasting docs.

import Soketi from '@soketi/soketi-js';

window.Soketi = new Soketi({
    host: window.location.hostname,
    key: 'echo-app-key', // should be replaced with the App Key
    authHost: 'http://127.0.0.1',
    authEndpoint: '/broadcasting/auth',
});

// for example
Soketi.channel('twitter').listen('.tweet', e => {
    console.log({ tweet: e.tweet });
});

Authorizing Sanctum

The package has full compatibility with the Pusher.js connector, meaning that you can specify the authorizer for the request:

window.Soketi = new Soketi({
    host: window.location.hostname,
    key: 'echo-app-key',
    authorizer: (channel, options) => {
        return {
            authorize: (socketId, callback) => {
                axios.post('/api/broadcasting/auth', {
                    socket_id: socketId,
                    channel_name: channel.name,
                })
                .then(response => {
                    callback(false, response.data);
                })
                .catch(error => {
                    callback(true, error);
                });
            },
        };
    },
});

Encrypted Private Channels

This feature works with Echo Server 5.4.0 and beyond.

Encrypted Private Channels are Pusher-like features, but they come integrated with this package and you are free to leverage them.

Below you will find an example:

window.Soketi = new Soketi({
    host: window.location.hostname,
    key: 'echo-app-key',
    encryptionMasterKeyBase64: 'vwTqW/UBENYBOySubUo8fldlMFvCzOY8BFO5xAgnOus=',
});

According to the Pusher docs, the key was generated using OpenSSL:

$ openssl rand -base64 32

The key is also needed in your backend broadcasting client. For Laravel, in broadcasting.php:

'socketio' => [
    'driver' => 'pusher',
    ...
    'options' => [
        ...
        'encryption_master_key_base64' => 'vwTqW/UBENYBOySubUo8fldlMFvCzOY8BFO5xAgnOus=',
    ],
],

You then are free to use the encrypted private channels:

Soketi.encryptedPrivate('top-secret').listen('.new-documents', e => {
    //
});

Catching any event

You can catch any event using .onAny():

Soketi.onAny((event, ...args) => {
    //
});

Catching errors

Sometimes the connection might throw errors:

Soketi.error(({ message, code }) => {
    //
});

🐛 Testing

npm run test

🤝 Contributing

Please see CONTRIBUTING for details.

🔒 Security

If you discover any security related issues, please email alex@renoki.org instead of using the issue tracker.

🎉 Credits

Readme

Keywords

Package Sidebar

Install

npm i @soketi/soketi-js

Weekly Downloads

174

Version

1.4.3

License

Apache-2.0

Unpacked Size

114 kB

Total Files

91

Last publish

Collaborators

  • rennokki