This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
socket.on('userEnteredChat', () => { | |
io.emit('refreshChatUsers'); | |
}); | |
socket.on('userLeftChat', () => { | |
io.emit('refreshChatUsers'); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this._socket.on('chatupdated', () => { | |
return refreshApex(this.wiredMessages); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
socket.on('transmit', () => { | |
io.emit('chatupdated'); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form class="send-message-form"> | |
<input type="text" class="message-input"> | |
<button type="submit">Send</button> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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 })) |