Git Product home page Git Product logo

channelssdk_js's Introduction

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);
});

channelssdk_js's People

Contributors

lisomatrix avatar

Stargazers

 avatar Tony Silva avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.