Skip to content

michael-redman/nodejschat

Repository files navigation

nodejschat

Copyright 2016 Michael Redman
IN GOD WE TRVST.

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. To view a copy of this license, visit

https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode

Commercial license inquiries email michael.redman.4@gmail.com


Install Linux archive dependencies:

#Debian
apt-get install nodejs npm

#CentOS
yum install nodejs npm

On whatever host will be your database server, also "apt-get install postgresql" or "yum install postgresql-server".


Install node dependencies:

#in this dir (same dir as package.json)
npm install


Configuration:

Create a database and import the schema and stored procedure with "cat schema.psql new_message.plpgsql | psql yourdatabasename". See the postgresql documentation for creating database users and databases.

Edit ws_server/site_conf.js, and optionally html/site_conf.js if you are going to use the example HTML client, to match your settings. If the "cert" value is there (not commented) then the servers will attempt to use SSL with the certificate, key, and (optionally) certificate chain settings you configure. Always use SSL for production.


Launching (on Debian the node.js executable name is "nodejs" instead of "node"):

Run websocket server (uses settings in ws_server/site_conf.js):

node cluster.js ws_server/server.js

Optionally, run HTTP server for HTML+javascript client (uses settings in html/site_conf.js):

node cluster.js html/server.js


User sessions:

It is the embedding application's responsibility to create a valid session key in the database table "sessions". Else the websocket server will reject connection. The fields are session_key (text), user_name (text), chat_room (integer).

Example SQL: insert into sessions values('1','Michael Redman',0);

Use SSL and use session keys that are cryptographically hard to guess, otherwise you will have no security whatsoever.


Connecting:

Example connect with command line test client (ws server on port 56412)

node test_client.js wss://compute.michael-redman.name:56412/?session_key=6aa8b 100 500

This command says make 100 socket connections to the server at the specified URL and port, using the specified session key, and each client socket should "say something" on the average once every 500 seconds.

Example URL for HTML client in browser:

https://yourhost.com:56411/chat?session_key=1cf9a304


Websocket API:

Once the websocket has been established and the server has authenticated the client, it will send a message containing the JSON "{ authenticated: true }". The JSON message may contain other fields not yet specified. The server may discard any messages it receives prior to authenticating the client, so the client should wait for the message confirming authentication before sending anything.

The chat messages are also in stringified JSON.

Messasges from client to server must contain the field "message", which shall be a string with the message text.

Messages from server to client have the fields "time", which is a timestamp of the format 2016-08-08T17:20:32.421Z, "user", which is the user name of who sent the messages, and "message" which is the message text.


IN GOD WE TRVST.