socketbus-js

Client library for connecting to SocketBus

Usage no npm install needed!

<script type="module">
  import socketbusJs from 'https://cdn.skypack.dev/socketbus-js';
</script>

README

SocketBus JavaScript Client

Getting Started

  1. Install the client
npm install socketbus-js --save

Create a SocketBus instance

import SocketBus from 'socketbus-js';

const socketBus = new SocketBus({
    // SocketBus' public key
    app_id: 'my-public-key',
    
    // Endpoint to authenticate channels
    authEndpoint: '/channels/auth',

    // Application namespace
    namespace: '',

    // User id to be able to send event to all but the sender
    user_id: null,

    // Event fired on Connect
    onConnect: () => {

    },

    // Function to parse all registred events
    formatEventName: (event, options) => {

    }
})

Laravel Echo

import { SocketBusLaravel } from "socketbus-js";
import LaravelEcho from "laravel-echo";

window.Echo = SocketBusLaravel(LaravelEcho, {
    app_id: 'my-app-id'
});

Echo.private('foods')
    .listen('NewFoodEvent', (payload) => {
        // code
    })

Joining Channels

const foodsChannel = socketBus.private('foods')
    .listen('new-food', (payload) => { /** callback */ })
    .listen('food-status', (payload) => { /** callback */ })

Leaving Channels

foodsChannel.leave()

End-to-end encryption

Socket-to-socket communication

foodsChannel.emit('new-food', {
    food: 'cupcake'
});

Whispering

Whispers are messages up to 128 bytes, that doesn't require Socket-to-socket communication to be enabled.

foodsChannel.whisper('food-status', 'hot');

Custom Authentication