Last active
March 19, 2020 19:31
-
-
Save ayoisaiah/1c8d9df369a1e7c385dd554000c39b96 to your computer and use it in GitHub Desktop.
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
require('dotenv').config(); | |
const express = require('express'); | |
const cors = require('cors'); | |
const bodyParser = require('body-parser'); | |
const { StreamChat } = require('stream-chat'); | |
const app = express(); | |
app.use(cors()); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: true })); | |
// initialize Stream Chat SDK | |
const serverSideClient = new StreamChat( | |
process.env.STREAM_API_KEY, | |
process.env.STREAM_APP_SECRET | |
); | |
app.post('/join', async (req, res) => { | |
const { username } = req.body; | |
const token = serverSideClient.createToken(username); | |
try { | |
await serverSideClient.updateUser( | |
{ | |
id: username, | |
name: username, | |
}, | |
token | |
); | |
} catch (err) { | |
console.log(err); | |
} | |
const admin = { id: 'admin' }; | |
const channel = serverSideClient.channel('commerce', 'live-chat', { | |
name: 'Live Chat', | |
created_by: admin, | |
}); | |
try { | |
await channel.create(); | |
await channel.addMembers([username, 'admin']); | |
} catch (err) { | |
console.log(err); | |
} | |
return res | |
.status(200) | |
.json({ user: { username }, token, api_key: process.env.STREAM_API_KEY }); | |
}); | |
const server = app.listen(process.env.PORT || 5500, () => { | |
const { port } = server.address(); | |
console.log(`Server running on PORT ${port}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment