This repository has been archived by the owner on Aug 18, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Hugo Müller-Downing
committed
Apr 25, 2018
0 parents
commit d9e1a6c
Showing
8 changed files
with
10,967 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}`); | ||
})(); |
Oops, something went wrong.