Skip to content

Instantly share code, notes, and snippets.

@jetaggart
Created August 3, 2020 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jetaggart/d6c93e15b7527e4e9ed3c5e4babbe8ab to your computer and use it in GitHub Desktop.
Save jetaggart/d6c93e15b7527e4e9ed3c5e4babbe8ab to your computer and use it in GitHub Desktop.
// backend/server.js:15
const streamClient = new StreamChat(
process.env.STREAM_API_KEY,
process.env.STREAM_API_SECRET
);
// ...
// backend/server.js:57
app.post("/stream-chat-credentials", async (req, res) => {
const { username } = req.body;
try {
let user = { id: username, name: username, role: 'user' };
await streamClient.upsertUsers([user]);
const channel = streamClient.channel('messaging', username, {
name: 'Sales Chat',
created_by_id: 'sales-admin'
});
await channel.create();
await channel.addMembers([username]);
const token = streamClient.createToken(user.id);
res.send({
userId: user.id,
userName: user.name,
channelId: channel.id,
token,
apiKey: process.env.STREAM_API_KEY
});
} catch (err) {
console.log(err);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment