This file contains 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
const util = require("util"); | |
const readline = require("readline"); | |
const { JSDOM } = require("jsdom"); | |
const { ChatManager, TokenProvider } = require("@pusher/chatkit"); | |
const axios = require("axios"); | |
const prompt = require("prompt"); | |
+const ora = require('ora'); | |
const main = async () => { | |
+ const spinner = ora(); |
This file contains 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
... | |
console.log(`Joined ${room.name} successfully`); | |
+ const input = readline.createInterface({input: process.stdin}); | |
+ input.on('line', async text => { | |
+ await currentUser.sendMessage({roomId: room.id, text}); | |
+ } |
This file contains 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
const { ChatManager, TokenProvider } = require('@pusher/chatkit') | |
const { JSDOM } = require('jsdom') | |
const util = require('util') | |
const prompt = require('prompt') | |
const axios = require('axios') | |
+ const readline = require('readline') |
This file contains 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
... | |
const room = availableRooms[chosenRoom] | |
+ await currentUser.subscribeToRoom({ | |
+ roomId: room.id, | |
+ hooks: { | |
+ onNewMessage: message => { | |
+ const { senderId, text } = message | |
+ if (senderId === username) return |
This file contains 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
availableRooms.forEach((room, index) => { | |
console.log(`${index} - ${room.name}`) | |
}) | |
+ const roomSchema = [ | |
+ { | |
+ description: 'Select a room', | |
+ name: 'room', | |
+ conform: v => { | |
+ if (v >= availableRooms.length) { |
This file contains 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
... | |
const currentUser = await chatManager.connect(); | |
+ const joinableRooms = await currentUser.getJoinableRooms(); | |
+ const availableRooms = [...currentUser.rooms, ...joinableRooms]; | |
+ console.log('Available rooms:'); | |
+ availableRooms.forEach((room, index) => { | |
+ console.log(`${index} - ${room.name}`); |
This file contains 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
await createuser(username); | |
+ const chatManager = new ChatManager({ | |
+ instanceLocator: 'YOUR_INSTANCE_LOCATOR', | |
+ userId: username, | |
+ tokenProvider: new TokenProvider({url: 'http://localhost:3001/authenticate'}), | |
+ }); | |
+ const currentUser = await chatManager.connect(); |
This file contains 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
const main = async () => { | |
try { | |
// Initialise prompt with an empty message | |
prompt.start(); | |
prompt.message = ''; | |
// Turn prompt.get() into a promise | |
const get = util.promisify(prompt.get); | |
// Create a schema to validate user input |
This file contains 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
+ const createUser = async username => { | |
+ try { | |
+ await axios.post('http://localhost:3001/users', {username}); | |
+ } catch ({message}) { | |
+ throw new Error(`Failed to create a user, ${message}`); | |
+ } | |
+ }; | |
const main = async () => { | |
... |
This file contains 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
const { ChatManager, TokenProvider } = require('@pusher/chatkit') | |
const { JSDOM } = require('jsdom') | |
const util = require('util') | |
const prompt = require('prompt') | |
+ const axios = require('axios') | |
... |
NewerOlder