Weโre building on another great Socket.IO Node.js basis web application project (with packagejson) by Damien Arrachequesne (of MIT) on GitHub, thanks, called โWhiteboardโ, which is a socket based collaborative web application โฆ downloaded and assembled into place on the MacBook Pro and compiled via โฆ
$ npm i && npm start
โฆ and (re)started via โฆ
$ node index
โฆ as per the usual Node.js way โฆ using an HTML(5) canvas to share the drawing of โฆ
- scribble based lines in a variety of colours โฆ and we added, today โฆ
- discrete click/touch lines in a wider variety of colours
- discrete click/touch rectangles
- discrete click/touch circles
- discrete click/touch top left image (via an image URL) placement
- discrete click/touch top left and bottom right image placement
Of course, where this can have useful implications are for games where, effectively, a single screen is shared and graphically collaborated to, by one or more users.
Whatโs the go integrating โฆ
- a new โdiscrete click/touchโ set of functionality โฆ into an existant โฆ
- scribble based line set of functionality
? Totally in mainjs (changed in thisway) we added/changed the jQuery Javascript event code as below โฆ
var numbermd = 0, lastwas = 0;
var prevx = 0, prevy = 0;
function onMouseDown(e){
numbermd++;
drawing = true;
lastwas = -1;
if (numbermd > 1) {
if (current.drawmode == 'rectangle') {
drawRectangle(prevx, prevy, e.clientX, e.clientY, current.color, true);
numbermd = 0;
} else if (current.drawmode == 'circle') {
drawCircle(prevx, prevy, e.clientX, e.clientY, current.color, true);
numbermd = 0;
} else if (current.drawmode == 'image2') {
drawImage(prevx, prevy, e.clientX, e.clientY, current.color, true);
numbermd = 0;
} else {
drawLine(prevx, prevy, e.clientX, e.clientY, current.color, true);
numbermd = 1;
}
} else if (current.drawmode == 'image1') {
drawImage(e.clientX, e.clientY, -1, -1, current.color, true);
numbermd = 0;
}
current.x = e.clientX;
current.y = e.clientY;
prevx = current.x;
prevy = current.y;
}
function onMouseUp(e){
if (!drawing) { return; }
if (lastwas != -1) numbermd = 0;
drawing = false;
if (lastwas != -1) drawLine(current.x, current.y, e.clientX, e.clientY, current.color, true);
lastwas = 0;
}
function onMouseMove(e){
if (!drawing) { return; }
numbermd = 0;
lastwas = 1;
drawLine(current.x, current.y, e.clientX, e.clientY, current.color, true);
current.x = e.clientX;
current.y = e.clientY;
}
Do you see a lot the same with the architecture of yesterdayโs Socket.IO Node.js Chat Application Primer Tutorial? They share the use of sockets. They share the collaborative screen sharing approach. They share the idea of a single server process, serving a lot of clients using a local Node web server URL of //localhost:3000 as well. Once you start identifying similarities in things, you build up thoughts regarding how to construct things, because youโve โseenโ something similar in the past. Various platforms have various strengths, and just because this collaborative screen sharing socket using approach has been great over the last two web applications, in our eyes, donโt think there is only ever one way to do things. Being introduced to alternative ways is not going backwards at any time, as long as you are strong in working out what the client wants, and short of that, with a flexible client, what you are comfortable to use as a development platform, so long as you have sussed out it is a physically possible alternative.
Other code parts were โฆ
You can also see this play out at WordPress 4.1.1โs Socket.IO Node.js Whiteboard Application Primer Tutorial.
Previous relevant Socket.IO Node.js Chat Application Primer Tutorial is shown below.
Network sockets are a powerful communication tool for software developers โฆ
A network socket is an internal endpoint for sending or receiving data at a single node in a computer network. Concretely, it is a representation of this endpoint in networking software (protocol stack), such as an entry in a table (listing communication protocol, destination, status, etc.), and is a form of system resource.
Team that with the brilliant Node.js local web server product (we last talked about with Node.js and Node Local Web Server Primer Tutorial) based real time engine socket.io code, and you can build an application, like todayโs chat application very speedily, and reliably โฆ thanks, everybody.
So, todayโs chat application โฆ
โฆ started as socket.ioโs โHello Worldโ โfoot in the doorโ (with that one prerequisite that you should install Node.js (as we did at that tutorial link above some time back)) that gets you places fast, then we recommend delving into their very interesting follow up suggestions, expressed by โฆ
- Hello
World โby the bookโ (//localhost:3000 Node.js local web server) package
json and index
js and index
js (remember how Node.js has Javascript client and Javascript server โฆ wow!!!) and index
html โฆ then โฆ
- after work on Hello World follow up questions index
js (changed in thisway) and index
html (changed in thisway) โฆ attending to โฆ
- Broadcast a message to connected users when someone connects or disconnects โฆ user messages are all prefixed by a user count and they can display a user list on request, as discussed below
- Add support for nicknames โฆ we start with a robotic (username) nomenclature (eg. [Chatter_1504844291]) and allow user to change name via a designated field or via โI am cedric popplesโ type of message
- Donโt send the same message to the user that sent it himself. Instead, append the message directly as soon as he presses enter. โฆ user can override this (sensible) functionality by placing themselves into an โ(only for [cedric popples])โ type of list as required
- Add โ{user} is typingโ functionality โฆ trap the jQuery onkeyup event logic as per $(โformโ).keyup(function(event) { [more code here] });
- Show whoโs online โฆ we make this a client action required piece of functionality
- Add private messaging โฆ we allow for โ(only for [cedric popples])โ and โ(not for [cedric popples])โ type lists
- Share your improvements! โฆ as linked to above, there is also a feature to adjust the message listโs โoddโ element colour via โฆ
CSS:
#messages li:nth-child(odd) { background: #eee; }
jQuery Javascript:
$('#imyc').change(function(){
$('#messages li:nth-child(odd)').css('background-color', $('#imyc').val());
document.getElementById('imyc').style.display='none';
});
HTML:
<input type="color" value="lightgray" id="imyc" style="display:none;" title="Colour of odd messages"></input>
โฆ and eventually the way of socket.io sinks in (the verdict? โฆ itโs great!), and you can see where its role might be in the Full-Stack Developer roles that package these software modules up, and which encourage the use of products like Ansible to manage deployment procedures. But for me the question still is โฆ can Socket.IO put the toaster on and can Node.JS get out the plate while Ansible answers all the clientโs difficult questions to end up with that โbreakfast before you know you wanted itโ project weโve let slip for the last month โฆ wouldnโt you know it, Vagrant up and walked off with the toast before it got to the client.
This web application design, along with so many other platforms, revolves around the concept of a local web server, and if you want to read about Socket.IO in this context, in relation to others weโve talked about in this โHello Worldโ frame of reference, the blog post to visit is Cassini++ WebServer Primer Tutorial.
Previous relevant Node.js and Node Local Web Server Primer Tutorial is shown below.
We love PHP for web server work, but readily acknowledge โฆ
- PHP is losing out, in popularity terms, to frameworks like Node.js (which we introduce to you today with a โHello Worldโ tutorial, where youโll see its Javascript (yes, Javascript), in (server side) action)
- Node.js is a framework that has Javascript running on the server side, a pretty powerful scenario
โฆ and so, a great divide in programming (frameworks) is developing. Personally, I like the division of โlanguageโ between PHP server code and Javascript client code, but can see the โcutenessโ of just thinking โJavascriptโ, and so, today, we show you a Node.js installation on our MacBook Pro using Mac OS X, though you will see, if you take a skeg at our slideshow that Node.js is very much cross-platform by nature.
If you read this blog, youโll (hopefully) have seen how fond we PHPโers are here regarding the โฆ
MAMP local web server on port 8888 supporting PHP and a MySql database on port 8889 on a MacBook Pro Mac OS X using web browsers
โฆ well with Node.js, the scenario is โฆ
Node local web server on port 3000 supporting Javascript (Node.js like) and a MySql database (still) on port 8889 (in our case because we co-exist with MAMP and have MAMP running and access its socket file to make the MySql connection) if co-existing with (running) MAMP or port 3306 (it looks like) if there is no MAMP involved
And so, today, we first show a client โfacingโ โHello Worldโ scenario of just showing โฆ โHello Worldโ โฆ a web application โfrontendโ if you will.
Then we got a MySql database involved โฆ a web application โbackendโ if you will โฆ and we hope if you are a regular reader at this blog โฆ even though this blog posting may have โrocked your worldโ with (Node.js) Javascript getting involved at the server side (though we did warn you with our very first Javascript DOM focused tutorial called JavaScript and the DOM Tutorial?! โฆ itโs taken a while for the โwheelโ to turn!) โฆ databases always involve server side code, and so we update the hello_worldjs to include showing the results of our MySql query โฆ
SELECT * FROM CIRCLE, POINT WHERE pointid=originid
โฆ linked, โback in the dayโ with this MySql Stored Procedures Primer Tutorial โฆ and after some dead ends where we didnโt know whether to have MAMP running or not โฆ the slideshow is โwarts โn allโ โฆ we end up finding out that Node/Node.js (Javascript)/MySql can co-exist and be an alternative to our cherished MAMP (local Apache web server)/PHP/MySql โpower tripletโ. To have a few methods to try things canโt be that bad โฆ huh?! Stay tuned please.
We (almost) leave you with the Open Source links that made the research (culminating in todayโs tutorial picture) possible โฆ thanks โฆ
-
Node.js installer download webpage โฆ you download, then for Mac OS X, run package installer
Useful regarding (first draft) Node.js connection to MySql example
Useful regarding Node.js connection to MySql showing how MAMP socket can be used for co-existance
Another player in this realm of thinking is Ruby (on Rails), and with this in mind, take a read of Ruby on Rails Primer Tutorial where port 3000 is used with a local web server arrangement, as well. Finally, if all this โweb serverโ talk interests you as much as us, weโve been compiling โweb serverโ talk, so to speak into this one Cassini++ WebServer Primer Tutorial blog posting, where Node.js thinking from today has been added โฆ Cassini++ ? โฆ Cassini is the simplest (Windows) local web server we can think of, and we work discussions up from that. That tutorial also mentions Mojito Primer Tutorial which also shows Javascript on both sides of the ledger with Node.js getting involved.
If this was interesting you may be interested in this too.
If this was interesting you may be interested in this too.
If this was interesting you may be interested in this too.