 This tutorial is a continuation of the first part of the tutorial series to create a simple chat application with node.js, so if you have not followed the first part of the tutorial, I suggest to follow it first, so you are not confused to follow this second part tutorial, I will add a lot of User Experience (UX), for our chat app more interesting, we just start the tutorial. #### What Will I Learn? - Add Style and Add Time - Add Username in Chat #### Requirements - Install nodejs - Javascript Intermediate, - Basic Css , Html #### Difficulty - Intermediate ### Add Style and Add Time Now we will add two things first time and add style to be more clear between the previous chat, and add some unique in the chat. <li><b>Styling Chat</b></li> I will remove the style list first to remove the black sphere <code>list-style:none;</code>, and give <code>width:35%;</code> , So I like to share the two chats, between chat before ny with the current chat. and I will also give background # f5f5f5 in <code>< body ></code> to make the chat balloon look more clear <pre> <code> body{ background:#F5F5F5; } #chats li{ margin: 10px 0px; width: 35%; list-style: none; }</code> </pre>  Next , I will make a background between the chat before and the current chat, and I will divide the two between the chat. We can use Css <b>: nth-child (even)</b> or <b>: nth-child (odd)</b>. <b>Chat List Odd</b> <pre> <code> #chats li:nth-child(odd){ background: white; clear: both; color: #4a4a4a; padding: 10px 20px; border-radius: 5px; -webkit-box-shadow: 0 10px 6px -6px #777; -moz-box-shadow: 0 10px 6px -6px #777; box-shadow: 0 5px 6px -6px #777; } </code> </pre> <b>Chat List Even</b> <pre> <code> #chats li:nth-child(even){ background: #DCF8C6; color: #4a4a4a; float: right; padding: 10px 20px; border-radius: 5px; -webkit-box-shadow: 0 10px 6px -6px #777; -moz-box-shadow: 0 10px 6px -6px #777; box-shadow: 0 5px 6px -6px #777; } </code> </pre>  and we will see the result like this, I give <code>float: right</code> there is list Even and clean the float effect<code>clear:both;</code> in list Odd, it is important to do to the list is not messy <li><b>Add Time</b></li> Next, to add User Experience , I will add chat time . I will use momen.js to make time format easier to read. We have to install momen.js first. <br> <code>npm install moment</code>  And for its use like this, in file <b>server.js</b> . <code>var moment = require('moment');</code> : We will import moment . <code>socket.emit('newMessage',msg, moment().fromNow());</code> : Now we will pass two variables, the first variable is the message and the second is the time. And on its frontend we can capture the parameters that are in the passing of the backend and bring it up. <b>html</b> ``` html socket.on('newMessage',function(msg,date){ $("#chats").append("<li>"+msg+"<span>"+date+"</span></li>"); }); ``` <br><br> <b>Css</b> I will styling the time <code>< span ></code> like this : <pre> <code> #chats li > span{ color: grey; font-size: 17px; } </code> </pre>    ### Add Username in Chat We see our chat app, there is no username chat, so we will not know who wrote that chat, well in this section I will add the name of the sender of the message. My first step will be to add name input in our application. <b>Html</b> ``` html <div class="wrapper-chat"> <form> <input class="input-username" type="text" id="username" placeholder="Name"> <input class="input-chat" type="text" id="text_chat" placeholder="Message"> <button class="button-chat">Send</button> </form> </div> ``` I added the input name <code>< input class="input-username" type="text" id="username" placeholder="Name" ></code> . <b>Css</b> <pre> <code> .input-chat{ width: 50%; height: 65px; border-radius: 40px; display: inline-block; border: none; padding: 0px 20px; font-size: 30px; color: #4a4a4a; } .input-username{ width: 14%; height: 65px; border-radius: 40px; display: inline-block; border: none; padding: 0px 20px; font-size: 30px; color: #4a4a4a; } .button-chat{ width: 20%; height: 65px; display: inline-block; float: right; background: white; border: none; border-radius: 10px; font-size: 17px; color: #0770cd; font-weight: bold; } </code> </pre>  Next step, we will add username in chat. The way is quite easy, we just need to pass the username to the backend and merge in the <b>msg</b> parameter . <b>Javascript</b> ``` html <script type="text/javascript"> var socket = io(); $('form').submit(function(){ var username = $('#username').val(); socket.emit('newMessage',username +'<br>'+ $('#text_chat').val()); $('#text_chat').val(''); $('#username').val('') return false }); socket.on('newMessage',function(msg,date){ $("#chats").append("<li>"+msg+" <span>"+date+"</span></li>"); }); </script> ``` <code>var username = $('#username').val();</code> : Store the value of the input <b># username</b> into the <b>var username</b> variable. <code>socket.emit('newMessage',username +'<br>'+ $('#text_chat').val());</code> : Combining username and message <b>username +'<br>'+ $('#text_chat').val()</b> in passing into parameter <b>msg</b> <code>$('#text_chat').val('');</code> : To empty Value Input Id <b># text_chat</b> after Submitted. <code>$('#username').val('');</code> : To empty Value Input Id <b># username</b> after Submitted. <pre> <code> socket.on('newMessage',function(msg,date){ $("#chats").append("<li>"+msg+" <span>"+date+"</span></li>"); }); </code> </pre> Append the message on the frontend.     If there is no error, then we will see the results as above, the end of this tutorial in this second part, we can add a style and give a better UX to the user. in the next section I will add other features. hopefully this tutorial can help you. thank you #### Curriculum - https://utopian.io/utopian-io/@alfarisi94/create-a-simple-chat-app-using-node-js-part1 <br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@alfarisi94/create-a-simple-chat-app-using-node-js-part2">Utopian.io - Rewarding Open Source Contributors</a></em><hr/>
author | alfarisi94 | ||||||
---|---|---|---|---|---|---|---|
permlink | create-a-simple-chat-app-using-node-js-part2 | ||||||
category | utopian-io | ||||||
json_metadata | {"community":"utopian","app":"utopian/1.0.0","format":"markdown","repository":{"id":27193779,"name":"node","full_name":"nodejs/node","html_url":"https://github.com/nodejs/node","fork":false,"owner":{"login":"nodejs"}},"pullRequests":[],"platform":"github","type":"tutorials","tags":["utopian-io","nodejs","socket","frontend","web"],"users":["alfarisi94"],"links":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1519839691/jfnxiv51jobtunknlaxw.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519833027/dzcnksoizuusok5hvde6.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519833664/mspwhwnar8ivq4xtid1e.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519836312/rf9gswqthkwhiwqa3s8d.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519837453/aws2kho5b7lcwouflnf0.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519837535/lpnbln4t7gbkfgnlzga7.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519837544/ksgrbadohvr3jzllermw.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519838503/zvhxd6m7uat739mx78sk.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519839485/qxio0xk5xdfwqdxc8kb3.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519839513/imqjcrnk3bujfyv7xfmi.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519839592/n7sdzw0cjgbluflfptgw.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519839701/lfkoeaqzlbvcoqge6pl4.png"],"image":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1519839691/jfnxiv51jobtunknlaxw.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519833027/dzcnksoizuusok5hvde6.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519833664/mspwhwnar8ivq4xtid1e.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519836312/rf9gswqthkwhiwqa3s8d.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519837453/aws2kho5b7lcwouflnf0.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519837535/lpnbln4t7gbkfgnlzga7.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519837544/ksgrbadohvr3jzllermw.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519838503/zvhxd6m7uat739mx78sk.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519839485/qxio0xk5xdfwqdxc8kb3.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519839513/imqjcrnk3bujfyv7xfmi.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519839592/n7sdzw0cjgbluflfptgw.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519839701/lfkoeaqzlbvcoqge6pl4.png"],"moderator":{"account":"laxam","time":"2018-02-28T20:42:39.140Z","reviewed":true,"pending":false,"flagged":false},"questions":[],"score":0} | ||||||
created | 2018-02-28 17:46:18 | ||||||
last_update | 2018-02-28 20:42:42 | ||||||
depth | 0 | ||||||
children | 3 | ||||||
last_payout | 2018-03-07 17:46:18 | ||||||
cashout_time | 1969-12-31 23:59:59 | ||||||
total_payout_value | 15.221 HBD | ||||||
curator_payout_value | 6.482 HBD | ||||||
pending_payout_value | 0.000 HBD | ||||||
promoted | 0.000 HBD | ||||||
body_length | 7,564 | ||||||
author_reputation | 5,678,893,550,406 | ||||||
root_title | "Create a Simple Chat App Using Node.js part2" | ||||||
beneficiaries |
| ||||||
max_accepted_payout | 1,000,000.000 HBD | ||||||
percent_hbd | 10,000 | ||||||
post_id | 41,180,345 | ||||||
net_rshares | 4,815,749,632,327 | ||||||
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
adol | 0 | 259,162,588,140 | 50% | ||
eric-boucher | 0 | 176,059,266,761 | 50% | ||
thatmemeguy | 0 | 4,649,291,182 | 50% | ||
orelmely | 0 | 15,643,871,169 | 100% | ||
dyancuex | 0 | 858,286,022 | 50% | ||
toninux | 0 | 655,149,866 | 50% | ||
jdc | 0 | 711,323,567 | 20% | ||
bargolis | 0 | 667,553,407 | 5% | ||
pipks | 0 | 218,368,388 | 50% | ||
ilyastarar | 0 | 74,229,924,894 | 50% | ||
flauwy | 0 | 71,315,987,065 | 35% | ||
herman2141 | 0 | 133,394,628 | 50% | ||
mahdiyari | 0 | 13,201,193,804 | 20% | ||
auliausu | 0 | 6,863,403,515 | 100% | ||
ronimm | 0 | 12,586,551,189 | 100% | ||
mufasatoldyou | 0 | 7,632,884,380 | 100% | ||
makrotheblack | 0 | 2,782,935,684 | 50% | ||
simonluisi | 0 | 2,138,912,115 | 100% | ||
thinkkniht | 0 | 500,079,142 | 75% | ||
jfuenmayor96 | 0 | 2,434,043,627 | 50% | ||
drigweeu | 0 | 316,586,830 | 3% | ||
jesdn16 | 0 | 2,848,918,877 | 100% | ||
xtramedium | 0 | 150,575,667 | 50% | ||
yandot | 0 | 7,477,921,439 | 30% | ||
stoodkev | 0 | 16,583,250,626 | 10% | ||
luisrod | 0 | 116,008,370 | 15% | ||
ansonoxy | 0 | 1,645,765,071 | 100% | ||
jamesbarraclough | 0 | 5,931,030,168 | 100% | ||
smafey | 0 | 810,904,905 | 50% | ||
hillaryaa | 0 | 1,110,506,237 | 100% | ||
espoem | 0 | 15,937,536,280 | 35% | ||
timmyeu | 0 | 744,716,702 | 50% | ||
gotgame | 0 | 6,321,796,591 | 100% | ||
maxwell95 | 0 | 298,058,023 | 50% | ||
zoneboy | 0 | 2,000,105,070 | 50% | ||
idlebright | 0 | 3,144,520,923 | 50% | ||
utopian-io | 0 | 3,950,476,320,359 | 2.11% | ||
afrin12 | 0 | 212,764,227 | 50% | ||
steaknsteem | 0 | 1,671,210,813 | 50% | ||
abdullahall | 0 | 215,431,940 | 50% | ||
masud222 | 0 | 267,866,799 | 50% | ||
parag | 0 | 220,652,863 | 50% | ||
kslo | 0 | 2,405,655,664 | 50% | ||
mrmaracucho | 0 | 554,718,436 | 100% | ||
nathalie13 | 0 | 1,120,791,989 | 100% | ||
gutzygwin | 0 | 359,351,338 | 25% | ||
not-a-bird | 0 | 1,742,267,100 | 20% | ||
adhew | 0 | 61,532,000 | 10% | ||
bitopia | 0 | 1,441,439,810 | 100% | ||
berkaytekinsen | 0 | 1,567,148,098 | 100% | ||
jutdagut | 0 | 423,829,845 | 100% | ||
zohaib715 | 0 | 323,350,749 | 50% | ||
evilest-fiend | 0 | 2,383,013,162 | 100% | ||
studytext | 0 | 144,840,585 | 25% | ||
enjoyy | 0 | 171,279,475 | 50% | ||
navx | 0 | 2,027,851,810 | 70% | ||
handfree42 | 0 | 230,108,466 | 50% | ||
ilovekrys | 0 | 221,423,797 | 50% | ||
oliwaw | 0 | 9,283,458,435 | 100% | ||
family.app | 0 | 128,987,828 | 100% | ||
maphics | 0 | 103,646,747 | 100% | ||
dethclad | 0 | 1,505,418,349 | 50% | ||
sebastiengllmt | 0 | 306,503,607 | 50% | ||
utopian-1up | 0 | 18,108,054,748 | 100% | ||
luoq | 0 | 10,402,055,150 | 100% | ||
zlatkamrs | 0 | 429,419,978 | 70% | ||
hrishikeshmatre | 0 | 260,818,240 | 50% | ||
amosbastian | 0 | 9,945,478,580 | 50% | ||
bobsthinking | 0 | 3,933,603,869 | 100% | ||
yourmercury | 0 | 864,032,858 | 100% | ||
acrywhif | 0 | 3,129,134,052 | 80% | ||
layanmarissa | 0 | 218,605,398 | 50% | ||
sweeverdev | 0 | 1,052,002,634 | 50% | ||
isacastillor | 0 | 1,246,054,143 | 95% | ||
rhotimee | 0 | 5,325,404,596 | 50% | ||
jrmiller87 | 0 | 1,938,929,427 | 100% | ||
solomon507 | 0 | 461,557,944 | 50% | ||
patatesyiyen | 0 | 78,453,580 | 12.5% | ||
deejee | 0 | 122,612,548 | 20% | ||
murez-nst | 0 | 1,298,187,480 | 100% | ||
onin91 | 0 | 170,456,166 | 50% | ||
neneandy | 0 | 3,499,522,693 | 50% | ||
isabella394 | 0 | 2,481,673,321 | 100% | ||
zinonweke | 0 | 2,047,371,804 | 75% | ||
emailbox19149 | 0 | 122,596,202 | 50% | ||
emirfirlar | 0 | 359,028,238 | 50% | ||
lemony-cricket | 0 | 10,680,850,551 | 20% | ||
yeswanth | 0 | 613,320,311 | 100% | ||
kaking | 0 | 224,528,970 | 50% | ||
exploreand | 0 | 5,923,584,403 | 25% | ||
anharismail | 0 | 889,890,887 | 100% | ||
petvalbra | 0 | 604,805,326 | 100% | ||
steemassistant | 0 | 432,584,215 | 100% | ||
olayhemy | 0 | 374,081,722 | 50% | ||
photohunter1 | 0 | 1,653,993,006 | 100% | ||
photohunter2 | 0 | 103,575,844 | 100% | ||
photohunter3 | 0 | 99,604,162 | 100% | ||
photohunter4 | 0 | 84,986,416 | 100% | ||
photohunter5 | 0 | 82,487,898 | 100% | ||
josh26 | 0 | 54,566,665 | 10% | ||
howtosteem | 0 | 3,176,324,380 | 100% | ||
sylinda | 0 | 221,017,935 | 50% | ||
kingsman2 | 0 | 156,818,244 | 50% | ||
jbeguna04 | 0 | 384,788,527 | 50% | ||
fmbs25 | 0 | 255,907,205 | 50% | ||
marketstack | 0 | 2,138,809,329 | 10% | ||
livsky | 0 | 315,735,184 | 50% | ||
badmusazeez | 0 | 104,436,225 | 50% | ||
historyfreak | 0 | 307,250,000 | 50% | ||
roj | 0 | 1,439,219,255 | 100% | ||
raoul.poenar | 0 | 2,014,608,674 | 50% | ||
kid1412 | 0 | 257,593,235 | 50% | ||
aderemi01 | 0 | 482,446,718 | 50% | ||
paav | 0 | 460,393,923 | 75% | ||
consultingpro | 0 | 205,952,402 | 50% | ||
killbill73 | 0 | 119,531,297 | 50% | ||
amirdesaingrafis | 0 | 220,667,864 | 50% | ||
fai.zul | 0 | 267,912,252 | 50% | ||
nightdragon | 0 | 169,341,630 | 50% | ||
jacintoelbarouki | 0 | 217,802,009 | 50% | ||
reazuliqbal | 0 | 597,575,117 | 100% | ||
aliyu-s | 0 | 250,505,622 | 50% | ||
mirna98 | 0 | 218,631,079 | 50% | ||
flinter | 0 | 159,366,622 | 50% | ||
abreu | 0 | 217,651,557 | 50% | ||
opulence | 0 | 1,792,302,303 | 50% | ||
phasma | 0 | 122,580,114 | 20% | ||
heshe-f | 0 | 247,275,732 | 25% | ||
donjyde | 0 | 227,333,837 | 50% | ||
crispycoinboys | 0 | 4,544,096,992 | 75% | ||
gwapoaller | 0 | 306,623,175 | 50% | ||
bluestorm | 0 | 453,605,948 | 75% | ||
genoner | 0 | 218,859,972 | 50% | ||
dexter24 | 0 | 218,540,676 | 50% | ||
muammarnst | 0 | 178,447,738 | 50% | ||
jayo | 0 | 232,927,190 | 50% | ||
statsexpert | 0 | 3,465,606,185 | 29.4% | ||
pepememes | 0 | 206,392,348 | 50% | ||
animesukidesu | 0 | 181,902,566 | 50% | ||
feronio | 0 | 233,026,403 | 50% | ||
ozcanpolat | 0 | 246,140,029 | 50% | ||
mellissamartz | 0 | 92,215,444 | 15% | ||
anime.lovers | 0 | 306,607,327 | 50% | ||
flugbot | 0 | 122,643,184 | 100% | ||
nikolap99 | 0 | 609,848,155 | 100% | ||
fel1xw | 0 | 120,122,257 | 100% | ||
palermo | 0 | 490,259,827 | 100% |
Thank you for the contribution. It has been approved. You can contact us on [Discord](https://discord.gg/uTyJkNm). **[[utopian-moderator]](https://utopian.io/moderators)**
author | laxam |
---|---|
permlink | re-alfarisi94-create-a-simple-chat-app-using-node-js-part2-20180228t204250455z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"} |
created | 2018-02-28 20:42:51 |
last_update | 2018-02-28 20:42:51 |
depth | 1 |
children | 0 |
last_payout | 2018-03-07 20:42:51 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 172 |
author_reputation | 4,021,844,514,967 |
root_title | "Create a Simple Chat App Using Node.js part2" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 41,215,005 |
net_rshares | 0 |
<div class="pull-left">  </div> <div class="text-justify"> ### You've got upvoted by <code>Utopian-1UP</code>! You can give up to ten [1UP](https://steemit.com/utopian-io/@steem-plus/steemplus-2-4-utopian-1up-is-here)'s to Utopian posts every day after they are accepted by a Utopian moderator and before they are upvoted by the official @utopian-io account. Install the @steem-plus browser extension to use 1UP. By following the 1UP-trail using [SteemAuto](https://steemauto.com/) you support great Utopian authors and earn high curation rewards at the same time. <hr> 1UP is neither organized nor endorsed by Utopian.io! </div>
author | utopian-1up |
---|---|
permlink | 20180301t124818582z |
category | utopian-io |
json_metadata | {"app":"1up"} |
created | 2018-03-01 12:48:18 |
last_update | 2018-03-01 12:48:18 |
depth | 1 |
children | 0 |
last_payout | 2018-03-08 12:48:18 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 764 |
author_reputation | 2,324,758,056,093 |
root_title | "Create a Simple Chat App Using Node.js part2" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 41,374,072 |
net_rshares | 0 |
### Hey @alfarisi94 I am @utopian-io. I have just upvoted you! #### Achievements - WOW WOW WOW People loved what you did here. GREAT JOB! - You have less than 500 followers. Just gave you a gift to help you succeed! - Seems like you contribute quite often. AMAZING! #### Community-Driven Witness! I am the first and only Steem Community-Driven Witness. <a href="https://discord.gg/zTrEMqB">Participate on Discord</a>. Lets GROW TOGETHER! - <a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1">Vote for my Witness With SteemConnect</a> - <a href="https://v2.steemconnect.com/sign/account-witness-proxy?proxy=utopian-io&approve=1">Proxy vote to Utopian Witness with SteemConnect</a> - Or vote/proxy on <a href="https://steemit.com/~witnesses">Steemit Witnesses</a> [](https://steemit.com/~witnesses) **Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x**
author | utopian-io |
---|---|
permlink | re-alfarisi94-create-a-simple-chat-app-using-node-js-part2-20180302t015233526z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"} |
created | 2018-03-02 01:52:33 |
last_update | 2018-03-02 01:52:33 |
depth | 1 |
children | 0 |
last_payout | 2018-03-09 01:52:33 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 1,143 |
author_reputation | 152,955,367,999,756 |
root_title | "Create a Simple Chat App Using Node.js part2" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 41,518,709 |
net_rshares | 0 |