Client NodeJS - XSockets/XSockets.NET-4.0 GitHub Wiki

##NodeJS To get the client just get the latest package from https://www.npmjs.com/package/xsockets.net The protocol used by the NodeJS client is available as of XSockets 4.2+

The NodeJS API is very new and currently supports RPC.

###Client Setup The NodeJS client has no dependencies, just install the latest package from npm by running

npm install xsockets.net

###How to establish a connection To get a connection just pass in the endpoint to the server.

var xsockets = require('./xsockets.net');
var c = new xsockets.TcpClient('127.0.0.1', 4502, ['foo']);

Note that the second parameter is an array of controllers to connect to. We only use one controller in this sample.

###How to configure the connection You have a few options when creating the connection, such as query strings and controllers.

####How to specify query string parameters TBD

####How to specify controllers Since you can multiplex over several controllers on one connection you can specify the controllers to use when you create the connection. If you would like to connect to three controllers A, B and C you would do:

var c = new xsockets.TcpClient('127.0.0.1', 4502, ['a','b','c']);

The names of the controllers is not case sensitive so A is the same as a.

###RPC

####How to set properties on the server from the client Since we have state on all controllers in the connection and can take advantage of that we can store information server side and not send trivial things like a user name each time we want to communicate.

Server

A simple model for a chat

public class ChatModel
{
    public string UserName {get;set;}
    public string Text {get;set;}
}

A controller where we use state to only send in text since the username is known.

//using XSockets.Core.XSocket;
//using XSockets.Core.XSocket.Helpers;

public class Chat : XSocketController
{
    public string UserName {get;set;}
    public void ChatMessage(ChatModel message)
    {
        message.UserName = this.UserName;
        this.InvokeToAll(message,"chatmessage");
    }
}

Client

conn.controller('chat').setProperty('username','David');

####How to call server methods from the client

Server

A simple model for a chat

public class ChatModel
{
    public string UserName {get;set;}
    public string Text {get;set;}
}

A controller where we use state to only send in text since the user name is know. See How to set properties on the server from the client

//using XSockets.Core.XSocket;
//using XSockets.Core.XSocket.Helpers;

public class Chat : XSocketController
{
    public string UserName {get;set;}
    public void ChatMessage(ChatModel message)
    {
        message.UserName = this.UserName;
        this.InvokeToAll(message,"chatmessage");
    }
}

Client

Since we already have set the property of UserName on the server we only need to send the Text property

conn.controller('chat').send('chatmessage',{Text:'Calling chatmessage on server and passing a part of the complex object'});

####How to define methods on the client that the server can call To define a method that the server can call from a Controllerjust add a method to the controller in JavaScript. The parameters can be complex objects.

Method name has to be lowercase in the JavaScript API. For example:

this.Invoke('Hello from server','ChatMessage'); 

on the server will execute chatmessage on the client.

Client

var conn = new xsockets.TcpClient('127.0.0.1', 4502, ['chat']);

conn.controller('chat').on('chatmessage', function(data){
    console.log(data.UserName + " - " + data.Text);
});

Server

A simple model for a chat

public class ChatModel
{
    public string UserName {get;set;}
    public string Text {get;set;}
}

A controller where we use state to only send in text since the user name is know. See How to set properties on the server from the client

//using XSockets.Core.XSocket;
//using XSockets.Core.XSocket.Helpers;

public class Chat : XSocketController
{
    public string UserName {get;set;}
    public void ChatMessage(ChatModel message)
    {
        message.UserName = this.UserName;
        this.InvokeToAll(message,"chatmessage");
    }
}

###How to handle connection lifetime events The events on connection level provide information about the socket being opened/closed. The controllers has their own lifetime events.

OnConnected

conn.onconnected = function(){
    console.log('socket connected');
};

OnDisconnected

TBD

###How to handle controller lifetime events The controller has nothing to do with the actual socket. These events tell you about the controllers you are using over your connection.

OnOpen Event

conn.controller("chat").onopen = function(ci){
    console.log('opened',ci);
};

OnClose Event

conn.controller("chat").onclose = function(ci){
    console.log('closed',ci);
};

Open a controller

As soon as you start to communicate over a new controller the OnOpen event will fire. You actually do not need to specify the controller in the connection. As long as the controller exists on the server the OnOpen event will fire.

Close a controller

To close as controller (not the actual connection/socket) you just call the Close method on the controller instance. This will fire the OnClose event on the controller.

conn.controller("chat").close();

###How to handle errors

Connection level

conn.onerror = function(err){
    console.log(err);
};

Controller level

conn.controller("chat").onerror = function(err){
    console.log(err);
};