How to Create Real-time Chat Application with Node.js Part 4
Публикувано: 2018-09-29 18:46:34
Finishing up the back-end: When a user disconnects from our app, we remove the saved nickname from Redis using the lrem method which we use to remove one occurrence of the value stored in the socket.nickname property. Then, we get all users online again and broadcast them to all connected.
1 2 3 4 5 6 7 |
socket.on("disconnect", function () { client.lrem('nicknames', -1, socket.nickname); client.lrange('nicknames', 0, -1, function (err, result) { io.sockets.emit('online', result);
}); }) |
Outside of our callbacks for database and socket connection, we serve the index.htmlfile when somebody accesses the root of our website:
1 2 3 |
app.get("/", function (req, res) { res.sendfile("public/views/index.html"); }) |
Submitting the app to Heroku
Heroku is a cloud application platform which allows you to upload applications in many different languages such Node.js, Ruby, PHP, Python, Java, Clojure and Scala. They have different tiers depending on the server load your application will cause but they also have a free tier that you can use when developing applications. To use Heroku, first you would have to install the Heroku toolbelt from: https://toolbelt.heroku.com/
A PATH variable would be added so you can just open a CLI (Terminal, cmd.exe, Powershell, whatever command line interface you have available) and type: heroku login.
Typing it and pressing Enter will ask for your credentials (email and password)
You would also need to have Git installed and preferably added to your PATH variables:
You have to enter the root directory of your app using something like: cd E:\Talkyand type git init
This will initialize an empty git repository in that directory. You need to have a package.json file that contains all dependencies of your app (such as Express and Redis). Our package.json file looks like that:
1 2 3 4 5 6 7 8 9 10 11 12 |
{ "name": "Talky", "version": "0.0.1", "description": "A real-time chat where messages come with audio", "author": "PHPGang", "dependencies": { "express": "^4.12.0", "html-entities": "^1.1.3", "redis": "^0.12.1", "socket.io": "^1.3.4" } } |
You do not have to upload the node_modules directory so you can just create a .gitignore file in the root of your app (it tells Git to ignore particular files/directories or wildcards when adding files to the version control) with the following text:
node_modules
Then, you type git add –allto add all new or modified files to the staging area followed by git commit –m “MESSAGE TO KNOW WHAT CHANGES YOU HAVE MADE”.
Finally, type git push heroku master, wait a bit and your app will be live at the URL that is going to be mentioned in the command-line interface.
You can find the sample application online at: https://infinite-brook-7325.herokuapp.com/
Part 1: How to Create Real-time Chat Application with Node.js Part 1
Part 2: How to Create Real-time Chat Application with Node.js Part 2
Part 3: How to Create Real-time Chat Application with Node.js Part 3
Author Ivan Dimov
Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: https://www.dimoff.biz. facebook, twitter
Article source: https://www.phpgang.com/how-to-create-real-time-chat-application-with-node-js-part-4_2181.html