Created
January 6, 2020 21:00
-
-
Save ayoisaiah/b05cd616f319ee541b1899933e68c9c0 to your computer and use it in GitHub Desktop.
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
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 })); | |
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, | |
}, | |
token | |
); | |
const admin = { id: 'admin' }; | |
const channel = serverSideClient.channel('team', 'chat', { | |
name: 'Group messaging', | |
created_by: admin, | |
}); | |
await channel.create(); | |
await channel.addMembers([username]); | |
} catch (err) { | |
res.status(500).json({ err: err.message }); | |
return; | |
} | |
return res.status(200).json({ user: { username }, token }); | |
}); | |
app.listen(7000, () => { | |
console.log(`Server running on PORT 7000`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment