📜 ⬆️ ⬇️

Remote procedure call in Node.js using Now.js

Introduction


For Node.js there is an excellent Socket.io library for cross-browser use of web sockets.
But for two-way interaction between the server and the client, one has to use the message model.
Using the Now.js library , you can transparently invoke client functions from the server and vice versa.

What it looks like


Now.js - Node.js module over Socket.io, providing the ability to call client functions from the server and vice versa. Also from the client and server side, you can not only call functions, but also use shared variables.
To begin, let's discuss the minimum code that is needed in order for us to make remote calls.

Server part

rpc = require " now "
rpc_channel
= rpc . initialize httpServer
rpc_channel
. now . myVariable = " some value "
rpc_channel
. now . distribute = ( message ) - >
rpc_channel
. now . receive @now . name , message
FractalizeR's HabraSyntax Source Code Highlighter .

What's going on here:
  1. Load the Now.js module
  2. Create a channel for calling methods, passing an initialize method to an instance of the server running Node.js (http.Server)
  3. In the resulting channel, there is an now object through which synchronization between the client and the server takes place. For example, the object contains the variable myVariable, which will be visible to clients.
  4. We place the distribute function in the now object, now clients can call it.
  5. In the distribute function, call the receive method on the client.

Client part

console . log ( now . myVariable ) ;
now
. receive = function ( message ) {
$
( " #messages " ) . append ( " <br> " + message ) ;
}
$
( " #send-button " ) . click ( function ( ) {
now
. distribute ( $ ( " #text-input " ) . val ( ) ) ;
} ) ;
FractalizeR's HabraSyntax Source Code Highlighter .

What's going on here:
  1. In some way we use the variable defined on the server.
  2. We define the function that the server can call (the now object is similar to the server one)
  3. In some place (in this case, in the event handler for the button press event) we call the server function

We write chat using Now.js and Kiss.js


Controller

kiss = require " kiss.js "
rpc
= kiss . controllers . rpc
class MyController
@on_app_started
= ( app ) - >
app
. rpc_channel . now . distributeMessage = ( message ) - >
gr
= rpc . getGroup @now . room
gr
. removeUser @user . clientId
gr
= rpc . getGroup @now . new_room
gr
. addUser @user . clientId
@now
. room = @now . new_room
gr
. now . receiveMessage @now . name , message
@
index = ( req , res ) - >
context
= { template_ name : " chat.html " }
v
= new kiss . views . TextViewer ( )
v
. render req , res , context
exports
. MyController = MyController
FractalizeR's HabraSyntax Source Code Highlighter .

At index, we simply generate a chat page, but the most interesting thing happens in on_app_started.
This method is executed when the application starts. Here we define the distributeMessage function that the client will call. When we call, we move the user to the room new_room and send a message to everyone.

Customer

$ ( document ) . ready ( function ( )
{
now
. name = " user " ;
now
. room = " room 1 " ;
function send ( )
{
now
. name = $ ( ' <div/> ' ) . text ( $ ( " #username " ) . val ( ) ) . html ( ) ;
now
. new_room = $ ( ' <div/> ' ) . text ( $ ( " #room " ) . val ( ) ) . html ( ) ;
var html = $ ( ' <div/> ' ) . text ( $ ( " #text-input " ) . val ( ) ) . html ( ) ;
now
. distributeMessage ( html ) ;
$
( " #text-input " ) . val ( " " ) ;
}
$
( " #send-button " ) . click ( send ) ;
$
( " #text-input " ) . keypress ( function ( e )
{
if ( e . keyCode = = 13 ) send ( ) ;
} ) ;
now
. receiveMessage = function ( name , message )
{
$
( " <li><h3> " + name + " </h3><p><strong> " + message + " </strong></p></li> " ) . prependTo ( " #messages " ) ;
$
( ' #messages ' ) . listview ( ' refresh ' ) ;
}
} ) ;
FractalizeR's HabraSyntax Source Code Highlighter .

When loading a document, specify the user name and the default room and create a function that will be called when the button is clicked. In this function, we get the name, room and message and send it all to the server.
We also define the function that the server will call to notify when a message is received.
The result can be seen here .
People in the room with the same name will see each other's messages, messages are not saved anywhere, as this is just a test example.
')
As you can see, using Now.js, we have the ability to synchronize variables and transparently call functions both from the server side and from the client side, which can be handy when building real-time applications.

Links


Demo
Chat source code
Now.js
Socket.io
Kiss.js

Source: https://habr.com/ru/post/130014/


All Articles