All Projects → TheVishnuKumar → lwc-streaming-api

TheVishnuKumar / lwc-streaming-api

Licence: BSD-2-Clause License
Lightning Web Component Streaming API

Programming Languages

javascript
184084 projects - #8 most used programming language
Apex
172 projects

Projects that are alternatives of or similar to lwc-streaming-api

one-pub-sub-lwc
One PubSub: A Declarative PubSub Library for Lightning Web Component and Aura Component
Stars: ✭ 19 (-24%)
Mutual labels:  salesforce, lwc, lightning-web-components, 0to1code, lightning-web-component
awesome-lwc
A list of interesting on platform Lightning Web Components resources and code examples
Stars: ✭ 124 (+396%)
Mutual labels:  salesforce, lwc, lightning-web-components
timeline-component-lwc
This component enables timeline view for Salesforce Record history.
Stars: ✭ 18 (-28%)
Mutual labels:  salesforce, lwc, lightning-web-components
PaymentForm
A form that takes credit card and address information. Uses a ported version of jessie pollack's card component.
Stars: ✭ 40 (+60%)
Mutual labels:  salesforce, lwc, lightning-web-components
lwc-soql-builder
Awesome SOQL execution tool developed in Lightning Web Components Open Source
Stars: ✭ 85 (+240%)
Mutual labels:  salesforce, lwc, lightning-web-components
apex-rollup
Fast, configurable, elastically scaling custom rollup solution. Apex Invocable action, one-liner Apex trigger/CMDT-driven logic, and scheduled Apex-ready.
Stars: ✭ 133 (+432%)
Mutual labels:  salesforce, lwc, lightning-web-components
quiz-host-app
Multiplayer quiz app built on Salesforce technology (host app)
Stars: ✭ 69 (+176%)
Mutual labels:  salesforce, lwc, lightning-web-components
lwc-redux
Integrate Redux with Lightning Web Component
Stars: ✭ 35 (+40%)
Mutual labels:  salesforce, lwc, lightning-web-components
sfdx-lwc-fullcalendarjs
Full Calendar JS - Lightning Web Components
Stars: ✭ 44 (+76%)
Mutual labels:  lwc, lightning-web-components
dreaminvest-lwc
Sample application for Lightning Web Components on Salesforce Platform. Part of the sample gallery. Financial services use case. Get inspired and learn best practices.
Stars: ✭ 41 (+64%)
Mutual labels:  salesforce, lightning-web-components
visualforce-to-lwc
A collection of code examples to help you move from Visualforce to LWC.
Stars: ✭ 92 (+268%)
Mutual labels:  salesforce, lightning-web-components
sfdx-lwc-jest
Run Jest against LWC components in SFDX workspace environment
Stars: ✭ 136 (+444%)
Mutual labels:  salesforce, lwc
build-apps-with-lwc
Ursus Park sample app for Trailhead project Build Flexible Apps with Lightning Web Components
Stars: ✭ 18 (-28%)
Mutual labels:  salesforce, lwc
lwc-modules
Build any LWC you want without ever having to touch Apex
Stars: ✭ 20 (-20%)
Mutual labels:  salesforce, lwc
Related-List-LWC
My first real foray into Lightning Web Components - an sobject / list view driven lightning data table
Stars: ✭ 21 (-16%)
Mutual labels:  salesforce, lwc
lightning-chatter-messenger
⚡ Lightweight Chatter messenger utility item, which supports real-time private conversation, in Salesforce Lightning Experience. Built by Lightning Web Component.
Stars: ✭ 33 (+32%)
Mutual labels:  salesforce, lightning-web-components
rcast
🎧 PWA podcast player written with LWC
Stars: ✭ 72 (+188%)
Mutual labels:  lwc, lightning-web-components
lwc-builder
VSCode Extension to kickstart Lightning Web Component development.
Stars: ✭ 15 (-40%)
Mutual labels:  lwc, lightning-web-components
fast-sfdc
A VSCode plugin to develop Salesforce projects in vscode
Stars: ✭ 16 (-36%)
Mutual labels:  salesforce, lwc
codeceptjs-bdd
Javascript BDD UI Automation Framework. Exclusive LWC Shadow DOM Support. Playwright, Webdriver.io, Appium, Saucelabs.
Stars: ✭ 35 (+40%)
Mutual labels:  salesforce, lwc

Update: Salesforce has launched Emp API from Summer 19 release. You can use this one as well. NOTE If you want to use streaming in Salesforce mobile app then EMP API still not available here. You can use lwc-streaming-api.

lightning/empApi

Deploy to Salesforce

Demo: https://www.youtube.com/watch?v=5yBWhLkybJ4

Features

  • Subscribe Push Topics, Platform Events.
  • Easy to use LWC APIs.
  • Easier control to subscribe, unsubscribe and check the status of the subscription.

Documentation

LWC Streaming API component let you subscribe streaming API channel in an easy way. You just need to provide the channel name. It message event let you get the payload from streaming API.

<c-lwc_streaming_api 
    channel="/topic/NewContactCreated" 
    api-version="45.0" 
    debug=true
    onmessage={handleMessage} 
    onerror={handleError} 
    class="lwc_streaming_api-1">
</c-lwc_streaming_api>

Attributes

This component has three types of attributes.

  1. channel: This is required attribute. Define the channel name. Ex: /topic/NewContactCreated and event/My_Event__e.

  2. api-version: This is an optional attribute. It defines that which API version will be used for cometd. If you omit this then it will take 45.0 as the default version.

  3. debug: This is an optional attribute. It takes the boolean value as the parameter. It allows you to see various logs on console. By default, this is set to false if you omit this.

Events

This component has two types of events.

  1. onmessage: This event fire when any streaming API sends the payload/message. You need to define the handler for your component to get the value from this event. You can get payload from this: event.detail.payload

  2. onerror: This event fire if any kind of error happens at the LWC streaming API component. You need to define the handler for your component to get the error message from this event. You can get the error from this: event.detail.error

Note: You can define debug=true to see all the console results as well.

Methods

This component has three types of methods that you can use to re-subscribe, unsubscribe and check the status of the subscription.

  1. subscribe(): Subscribe the channel if it was destroyed or unsubscribe. You cannot Subscribe a channel if it already Subscribed. It prevents the multiple payload event from streaming API.

  2. unsubscribe(): Unsubscribe the channel. You can Subscribe a channel if it is unsubscribed. We can use this to stop receiving payloads.

  3. checkConnection(): This method returns true or false. If the channel is subscribed then it will return the true else false.

Example

Step 1. Create a push topic using the Developer Console. Copy the following code and execute in the developer console. What will it do? It will create a push topic name NewContactCreated. Whenever a contact record gets created. It will send the payload to the onmessage event.

PushTopic pushTopic = new PushTopic();
pushTopic.Name = 'NewContactCreated';
pushTopic.Query = 'select Id,Name from Contact';
pushTopic.ApiVersion = 45.0;
pushTopic.NotifyForOperationCreate = true;
pushTopic.NotifyForOperationUpdate = false;
pushTopic.NotifyForOperationUndelete = false;
pushTopic.NotifyForOperationDelete = false;
pushTopic.NotifyForFields = 'All';
insert pushTopic;

Step 2. Create a Lightning Web Component name as lwc_streaming_demo. Copy and paste the following code to the files. Note: Add the target configuration in the meta XML file. so you can add your demo component to the using the app builder. In my case, I have added the component to the home page.

lwc_streaming_demo.js

import { LightningElement,track } from 'lwc';

export default class Lwc_streaming_demo extends LightningElement {

    @track error = '';
    @track payload = '';
    @track isConnectionOn;

    //Handles the error
    handleError(event){
        //Error is coming in the event.detail.error
        this.error = JSON.stringify(event.detail.error);
    }

    //Handles the message/payload from streaming api
    handleMessage(event){
        //Message is coming in event.detail.payload
        this.payload = this.payload + JSON.stringify(event.detail.payload);
    }

    //This method is subscribing the channel
    restart(){
        this.template.querySelector('.lwc_streaming_api-1').subscribe();
    }

    //This method is unsubscribing the channel
    destroy(){
        this.template.querySelector('.lwc_streaming_api-1').unsubscribe();

        this.payload = '';
        this.error = '';
    }

    //This method is checking if the channel is subscribed or not
    checkConnection(){
        this.isConnectionOn = this.template.querySelector('.lwc_streaming_api-1').checkConnection();
    }
}

lwc_streaming_demo.html

<template>
    <c-lwc_streaming_api 
        channel="/topic/NewContactCreated" 
        api-version="45.0" 
        debug=true
        onmessage={handleMessage} 
        onerror={handleError} 
        class="lwc_streaming_api-1">
    </c-lwc_streaming_api>

    <lightning-button label="Destroy Connection"  onclick={destroy}></lightning-button>

    <lightning-button label="Restart Connection"  onclick={restart}></lightning-button>

    <lightning-button label="Check Connection"  onclick={checkConnection}></lightning-button>

    <div style="background: white;padding: 50px;">
        <div style="margin:20px;">
            Payload
            <br/>
            {payload}
        </div>
        
        <div style="margin:20px;">
            Error:
            <br/>
            {error}
        </div>

        <div style="margin:20px;">
            Is Connection On:
            <br/>
            {isConnectionOn}
        </div>
    </div>
</template>

lwc_streaming_demo.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="lwc_streaming_demo">
    <apiVersion>45.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__HomePage</target>
    </targets>
</LightningComponentBundle>

Step 3. Open your demo component. Now create some contact records and you will see the message/payload on the screen.

Step 4 (Optional): If you want to try it with platform events. Create a platform event object. Subscribe it using below code.

<c-lwc_streaming_api 
        channel="/event/Test_Event__e" 
        api-version="45.0" 
        debug=true
        onmessage={handleMessage} 
        onerror={handleError} 
        class="lwc_streaming_api-1">
    </c-lwc_streaming_api>

Here Test_Event__e is my platform event object name.

Now create platform event records using below code:

Test_Event__e te = new Test_Event__e ();
te.Test_Field__c  = 'test data';
Database.SaveResult sr = EventBus.publish(te);

I have used a custom text field (Test_Field__c) in the platform event.

Code on gist

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].