Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

Commit

Permalink
Initial commit 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Müller-Downing committed Apr 25, 2018
0 parents commit d9e1a6c
Show file tree
Hide file tree
Showing 8 changed files with 10,967 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules/
28 changes: 28 additions & 0 deletions README.md
@@ -0,0 +1,28 @@
# Pusher Relay Chat 💬
> Terminal chat application built with Pusher Chatkit! 🚀
## Environment Variables
### Server
* `INSTANCE_LOCATOR`
* `SECRET_KEY`

### Client
* `INSTANCE_LOCATOR`

## Server
Run the auth server via:
```
cd api/
npm i
npm start
```

## Client
Create a user/room via the inspector in the Pusher Dashboard.

Run the client via:
```
cd api/
npm i
node index.js userId roomId
```
81 changes: 81 additions & 0 deletions api/index.js
@@ -0,0 +1,81 @@
const Hapi = require('hapi');
const Joi = require('joi');
const Chatkit = require('pusher-chatkit-server');

const {log} = console;

(async () => {
const {INSTANCE_LOCATOR: instanceLocator, SECRET_KEY: key} = process.env;
if (!instanceLocator || !key) {
log('INSTANCE_LOCATOR or CHATKIT_KEY not set in environment.');
process.exit(1);
}

const chatkit = new Chatkit.default({
instanceLocator,
key,
});

const server = Hapi.server({
host: '127.0.0.1',
port: 3000,
routes: {cors: true},
});

server.route([
{
method: 'POST',
path: '/auth',
options: {
validate: {
query: {
user_id: Joi.string().required(),
},
payload: {
grant_type: Joi.string().required(),
},
},
},
handler: async (request, h) => {
const {query: {user_id}, payload: {grant_type}} = request;
try {
const {body, status} = await chatkit.authenticate({
grant_type,
userId: user_id,
});
return h.response(body).code(status);
} catch (err) {
log(err);
throw err;
}
},
},
{
method: 'POST',
path: '/users',
options: {
validate: {
payload: {
name: Joi.string().required(),
},
},
},
handler: async (request, h) => {
const {payload: {name}} = request;
try {
const data = await chatkit.createUser({
id: name,
name,
});
return h.response(data).code(201);
} catch (err) {
log(err);
throw err;
}
},
},
]);

await server.start();
log(`Server running on ${server.info.host}:${server.info.port}`);
})();

0 comments on commit d9e1a6c

Please sign in to comment.