The Main of NowJS performance lies in the now item. The now object is Exclusive since it exists on the server plus the consumer.
This suggests variables you set in the now item are immediately synced involving the consumer and the server. Also server capabilities may be instantly referred to as on the customer and client capabilities may be termed directly from the server.
You might have a Performing HTTP server up and operating in NodeJS with just a couple strains of code. By way of example:
var http = require('http');
http.createServer(purpose (req, res)
res.writeHead(two hundred, 'Articles-Style': 'text/simple');
res.stop('Hello Worldn');
).hear(8080);
This small snippet of code will make an HTTP server, listen on port 8080, and send out back "Hi Entire world" for every request. Which is it. Practically nothing more essential.
Using NowJS, conversation concerning the consumer and server facet is just as uncomplicated.
Client Aspect:
On this code snippet, the shopper side sets a variable to 'someValue' and phone calls serverSideFunction(), which is declared only within the server.
Server Facet:
Everybody.now.serverSideFunction = purpose()
console.log(this.now.clientSideVariable);
The server aspect is then ready to accessibility clientSideVariable, and that is declared only around the customer.
All the main points including developing connections and communicating modify of knowledge in between the server and customer are handed automagically by the framework.
In reality crafting code utilizing this framework is so basic, the NowJS good day planet illustration is a Doing work chat consumer and server published in under a dozen lines of code. Go test it out.
As a straightforward work safe deposit box for sale out to have snug Using the NowJS API, we will modify the chat shopper example to help many chat rooms. Let us Check out how effortless it is actually.
Server Side (multiroom_server.js)
1. The very first thing we must do is modify the distributeMessage() function to only ship messages to people in the same chat space as being the person.
// Send concept to All people during the customers group
Absolutely everyone.now.distributeMessage = functionality(message)
var group = nowjs.getGroup(this.now.serverRoom);
group.now.receiveMessage(this.now.identify+'@'+this.now.serverRoom, information);
;
We store the name on the server place on the client aspect (this.now.serverRoom). If the shopper phone calls the distributeMessage() function we deliver the concept to All people in a similar chat room by utilizing getGroup() and using the team.now item in lieu of theeveryone.now object. (everyone is just a group that contains all people linked to the server).
2. Subsequent we need to take care of the customer switching chat rooms.
everyone.now.changeRoom = perform(newRoom)
var oldRoom = this.now.serverRoom;
//if previous area will not be null; then go away the aged room
if(oldRoom)
var oldGroup = nowjs.getGroup(oldRoom);
oldGroup.removeUser(this.user.clientId);
// be a part of The brand new place
var newGroup = nowjs.getGroup(newRoom);
newGroup.addUser(this.person.clientId);
// update the shopper's serverRoom variable
this.now.serverRoom = newRoom;
;
The getGroup() method fetches the group item if it exists and results in a gaggle if it isn't going to exist already. We utilize the teams addUser() and removeUser() methods to move the client from your previous room to the new area.
That is about this to the server facet.
Shopper Facet (multiroom.html)
three. First we add a drop down Using the list of server rooms.
four. Following we phone the server aspect changeRoom() operate if the consumer 1st connects and Each time the drop down is modified.
// on setting up 'now' link, set the server space
now.ready(function()
// By default pick the first chatroom
now.changeRoom($('#server-room').val());
);
// On change of fall down, apparent textual content and alter server place
$('#server-area').adjust(operate()
$("#messages").html(");
now.changeRoom($('#server-area').val());
);
5. For additional credit score, we are able to allow the server to dynamically provide the listing of rooms in the event the shopper connects.