This repository holds the code that was used to demo Websockets in the January 4th Knowledge Share. It is a low-fi build of a Chatroom which proves out:
- the ability of a Front End client to use Websockets to be notified by the server of relevant events & update the DOM accordingly, with less overhead computational expenses
- server-side Websocket filtering of messages: only the intended recipients are notified of messages
- exposing a REST API to a Websocket for simpler 3rd-party integrations
The demo uses Node v10.5.0 and NPM v.6.4.1
The demo also uses ngrok
which tunnels the port at which our Websocket lives so it can be exposed to the demo attendees. This requires an extra set-up step & allows you to expose a port on your localhost, but it is purely optional. To disable it, simply replace the ngrok
link with a link to localhost:8999
to connect. You can download it here.
- Clone the repository onto your machine
- If you wish to use
ngrok
to tunnel your local machine's port through ngrok's server, then make sure to download ngrok. - Via your terminal,
cd
intowebsocket
& runnpm install & node websocket.js
. This will run the websocket code on your port 8999. - (OPTIONAL) If you want to use
ngrok
then, from the directory where you have ngrok downloaded, run./ngrok http 8999
. Then go to Line 14 ofwebsocket-connection.service.ts
and replace thelocalhost:8999
of thewebSocketAddress
variable with the URL obtained from NgROK. For example, it can be set to:wss://1dc1d163.ngrok.io/?id=
- To start the app, from
app/websocketdemo
:npm install & npm start
- To start the REST API, from
REST_API
:npm install & node api.js
Once each of the 3 projects are running (the Websocket, the App, and the API), open multiple windows on your favorite browser to the App at localhost:4200
- each will represent a different chatbot user. Enter a different userName for each and then send messages from each account. If the recipient field is blank, the message will be sent to the channel. Observe the other windows be updated rapidly with the information that is intended for them.
Now imagine we want to expose a REST API to this chatroom, so 3rd party consumers can send messages without needing to go through our interface or create their own Websocket connection. That is precisely what our API offers. By sending the following payload attached to a POST request to localhost:3000
, which is the port the API is running on, this 3rd party can achieve this. The API itself maintains a Websocket connection to the Websocket & simply forwards the request payload.
{
"body": "Hellooooo",
"sender": "Hi",
"toEntireChat": false,
"recipientId": "Yichi"
}
https://www.youtube.com/watch?v=vQjiN8Qgs3c