1.0.19 • Published 3 years ago

channels_sdk v1.0.19

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Channels JavaScript SDK

Check the main project here

If you want to connect to channels with your browser, then this is the right place for you! First get the code from here and check the authentication section for creating a JWT Token.

After getting the token, initialize the SDK with:

let channelsSDK = new ChannelsSDK({ 
    url: '://url:port', // Don't fill behind the ://
    appID: 'AppID',
    token: 'JWT Token',
    secure: false // If should be WSS or WS and HTTPS or HTTP
});

You need to keep the object around and using for almost everything


Getting your channels

To get the client channels you can retrieve the channels that are public or the ones that are private. To do that just use the following:

The package works in browser, it uses the native WebSocket, if you can replace it, then it can work on Node

channelsSDK.fetchPublicChannels().then(channels => { // Replace public with private for private channels
});

Listening for new channels and removed channels

You can know you when lost access to a channel for received with:

channelsSDK.setOnChannelAdded((channelID) => {
    // When you receive access to a channel
})

channelsSDK.setOnChannelRemoved((channelID) => {
    // When you lose access to a channel
})

Working with a Channel

The object Channel is the object you will use the most, with it you can subscribe, publish, get other clients presence and get events.

First, in order to get a instance you can get from getPublicChannels() or getPrivateChannels() or after you get the public or private channels you can get one with channelsSDK.getChannel("channelID");, if none is found it will return null!

Once you have a Channel you subscribe with:

channel.subscribe(() => {
    // Callback for when the subscribe is confirmed
})

And you can get events with:

channel.setOnMessage((event) => {
    // Callback for the event
});

For presences events you have:

// On initial status from the channel
channel.setOnInitialStatusUpdate(() => {
    // You can get the presence with
    channel.getPresencesStatus();
});

// When status changes
channel.setOnOnlineStatusUpdate((statusUpdate) => {
    // The user status that changed
});

// When a user is added to the channel
channel.setOnJoin((join) => {

});

// When a user is removed from the channel
channel.setOnLeave((leave) => {

});

Publishing

To publish messages is simple as:

    channel.publish("my_event", "my_payload_can_be_json", () => {
        // Published confirmation
    });

    // If you don't want the confirmation or don't want the event to be stored pass null on the callback
    channel.publish("my_event", "my_payload_can_be_json", null);

!> Important: If your event doesn't request a confirmation, Channels will consider that the event is not important to store on channels with persistence enabled!


Getting last events

For getting events it's as simple as:

You can check other types of getting events on the section Synchronization

// Get last X (5 in this case) events 
channel.fetchLastEvents(5).then(events => {
    console.log(events);
});

// Get X (5 in this case) since given timestamp (inclusive)
channel.fetchLastEventsSince(5, 1615570823).then(events => {
    console.log(events);
});

// Get events since timestamp (inclusive)
channel.fetchEventsSince(1615570823).then(events => {
    console.log(events);
});

// Since timestamp to Up to timestamp (inclusive)
channel.fetchEventsBetween(1615570823, 1615570823).then(events => {
    console.log(events);
});
1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago