Broad Network


Node.js Socket Functions and Events

Internet Socket and ECMAScript - Part 2

Writing ECMAScript Module

Foreword: In this part of the series, I talk about Node.js functions and events for the socket, to be used with ECMAScript.

By: Chrysanthus Date Published: 24 Jul 2016

Introduction

The server script language for the library, Node.js is ECMAScript. In this part of the series, I talk about Node.js functions and events for the socket, to be used with ECMAScript. You should have read the previous part of the series before coming here, as this is a continuation.

Before you can have a socket in Node.js, you need to include the module, net, with a statement like:

    net = require('net');

Functions
net has methods that you can call, functions.

Returning a Socket
There are serveral ways of returning a client socket. You can use the following syntax:

    net.createConnection(port[, host][, connectListener])

where port is the port number, e.g. 55555 and  host is the host e.g. localhost or yahoo.com. I will talk about the optional [, connectListener] later. Example:

    socket = net.createConnection(55555, localhost);

where socket is a name of your choice.

The syntax to return a server socket is:

    net.createServer([options][, connectionListener])

A server has to be on first, before the client is connected to it. Here, [connectionListener] is a callback function used to send data to and from the client; the argument to the callback function is the socket of the client – see example in the next part of the series.

Listening
When a server (socket) has been created, it has to be listening for connection from the client. A syntax to do this is:

    server.listen(port[, hostname][, backlog][, callback])

where server is the server socket, a name of your choice. port is the port number e.g. 55555. hostname is the IP address of the server (computer). Node.js comes with a module called, DNS. This module can be used to convert hostname, e.g. localhost or yahoo.com to the appropriate IP address.

Backlog is the maximum length of the queue of pending connections. The default number is 511.  I will talk about callback later.

Closing the Socket
To close a client socket, you use:

        socket.end();

To close a server, you use:

        server.close();

Events

The On Data Event
The client is essentially a socket. The server is a kind of socket that works with the client socket. When data arrives at the socket, the on-data event is triggered. It can be handled as follows:

    socket.on('data', (data) =>
        {
            //receive and send data to other end
        });

The received data is a node.js buffer (array of bytes). It can be converted to a string as follows:

            receivedStr = data.toString();

where receivedStr is a name of your choice.

Converting String to Node Buffer
You can send data to the other end as a string or as a node.js buffer. The following code converts a string to a node.js buffer:

        givenString = 'some text';
        nodeBuf = new Buffer(givenString);

The On Error Event
When an error occurs in the socket activity, an on-error event is triggeres. It can be handled as follows:

        socket.on('error', (err) =>
            {
                console.log(err);
                socket.end();
            });

or in the case of the server, as follows:

    server.on('error', (err) =>
        {
            console.log(err);
            server.close();
        });

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Links

Internet Socket and ECMAScript
Handling Bytes in ECMAScript
ECMAScript Bitwise Operators
ECMAScript Module Essentials
More Related Links
Node Mailsend
EMySQL API
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module
ECMAScript Course

BACK NEXT

Comments

Become the Writer's Follower
Send the Writer a Message