Skip to content

Instantly share code, notes, and snippets.

@jamigibbs
jamigibbs / websocketChat.js
Last active Mar 31, 2020
Enter and leave chat
View websocketChat.js
handleEnterChat() {
setUserChatActive()
.then((res) => {
this.isChatActive = res.Chat_Active__c;
this._socket.emit('userEnteredChat');
return refreshApex(this.wiredChatUsers);
})
.catch(error => {
// eslint-disable-next-line no-console
console.error('handleEnterChat error', error);
@jamigibbs
jamigibbs / websocketChat.js
Created Mar 30, 2020
chat updated socket event
View websocketChat.js
this._socket.on('chatupdated', () => {
return refreshApex(this.wiredMessages);
});
View websocketChat.js
this._socket.on('output', (data) => {
if (data) {
const fields = {};
fields[CONTENT_FIELD.fieldApiName] = data.message;
fields[USER_FIELD.fieldApiName] = this.userId;
const message = { apiName: MESSAGE_OBJECT.objectApiName, fields };
createRecord(message)
.then(() => {
this._socket.emit('transmit');
@jamigibbs
jamigibbs / server.js
Last active Mar 30, 2020
Server socket on input
View server.js
socket.on('transmit', () => {
io.emit('chatupdated');
});
View message-submitted.js
import { getRecord } from 'lightning/uiRecordApi';
import MESSAGE_OBJECT from '@salesforce/schema/Chat_Message__c';
import CONTENT_FIELD from '@salesforce/schema/Chat_Message__c.Content__c';
import USER_FIELD from '@salesforce/schema/Chat_Message__c.User__c';
// ....
const messageInput = this.template.querySelector('.message-input');
messageInput.addEventListener('keydown', (event) => {
View message-form.html
<form class="send-message-form">
<input type="text" class="message-input">
<button type="submit">Send</button>
</form>
@jamigibbs
jamigibbs / websocketChat.js
Last active Mar 31, 2020
Socket.io import
View websocketChat.js
import { LightningElement, api, wire } from 'lwc';
import { loadScript } from 'lightning/platformResourceLoader';
import SOCKET_IO_JS from '@salesforce/resourceUrl/socketiojs';
import USER_ID from '@salesforce/user/Id';
// Your Heroku server link as a custom label and looks something like this:
// wss://my-heroku-app.herokuapp.com/
import WEBSOCKET_SERVER_URL from '@salesforce/label/c.websocket_server_url';
import getTodayMessages from '@salesforce/apex/ChatController.getTodayMessages';
View ChatController.cls
public with sharing class ChatController {
@AuraEnabled(cacheable=true)
public static List<Chat_Message__c> getTodayMessages() {
List<Chat_Message__c> messageList;
try {
messageList = [
SELECT Id, Content__c, CreatedDate, User__r.Name, User__r.MediumPhotoUrl
FROM Chat_Message__c
@jamigibbs
jamigibbs / server.js
Last active Mar 30, 2020
Node Websocket Server Base
View server.js
'use strict';
const express = require('express');
const socketIO = require('socket.io');
const PORT = process.env.PORT || 3000;
const INDEX = '/index.html';
const server = express()
.use((req, res) => res.sendFile(INDEX, { root: __dirname }))
View getPageRef.js
import { LightningElement, wire } from 'lwc';
import { CurrentPageReference } from 'lightning/navigation';
export default class PageRefDemo extends LightningElement {
@wire(CurrentPageReference)
currentPageReference;
handleDisplayPageRefObj(){
const pageRefObj = JSON.stringify(this.currentPageReference, null, 4);