Skip to content

Instantly share code, notes, and snippets.

@RizkyRajitha
Last active June 29, 2019 05:00
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 RizkyRajitha/f7e37652f7c0afe58b914f09a29b859d to your computer and use it in GitHub Desktop.
Save RizkyRajitha/f7e37652f7c0afe58b914f09a29b859d to your computer and use it in GitHub Desktop.
//import all the dependencies
const chatkit = require("@pusher/chatkit-server");
const express = require("express");
const bp = require("body-parser");
const cors = require("cors");
//make our server
const app = express();
//enable cors
app.use(cors());
//enable body parser
app.use(bp.urlencoded({ extended: false }));
app.use(bp.json());
//create chatkit object user - our credentials in the chatkit dashboard
const chatkitserver = new chatkit.default({
instanceLocator: "your-instanceLocator",
key:
"your-key"
});
//cofigure user route
app.post("/user", (req, res) => {
const datain = req.body;
console.log("requested username- "+datain.username);
chatkitserver
.createUser({ id:datain.username, name: datain.username })
.then(() => {
console.log("user created");
res.status(200).json({msg:"user_created"})
})
.catch(err => {
if (err.error === "services/chatkit/user_already_exists") {
console.log("duplicate user detected");
res.status(200).json({msg:"duplicate_user"})
}
//console.log(err);
});
});
//cofigure auth route
app.post("/auth", (req, res) => {
console.log("request connection by - " + req.query.user_id);
var usrid = req.query.user_id;
var authdata = chatkitserver.authenticate({ userId: usrid });
console.log(authdata);
res.status(authdata.status).json(authdata.body);
});
//listn to incoming requests on port 3005
app.listen(3005, () => {
console.log("server is listning on port 3005");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment