Friday, March 5, 2021

How to Create WebSocket Server & Client using Node.JS and JavaScript


You can follow the syntax below to create Websocket Server using Node.JS and WebSocket Client using JavaScript.

websocker-server.js

/*
	NodeJS WebSocket Server Example
	--------------------------------------------------

	Step 1 : $ npm install websocket
	Step 2 : Write Code
	Step 3 : Run
		 $ node websocket-server.js
	Step 4 : Done

	--------------------------------------------------
	Reference :
		- https://www.npmjs.com/package/websocket
		- https://www.pubnub.com/blog/nodejs-websocket-programming-examples/
*/

const http = require('http');
const WebSocketServer = require('websocket').server;
const server = http.createServer();

server.listen(9898, () => {
	console.log('WebSocket Server at localhost:9898')
});

const wsServer = new WebSocketServer({
	httpServer: server
});

wsServer.on('request', function(request) {
	const connection = request.accept(null, request.origin);

	console.log('\n\n>> REQUEST : ');
	console.log(request);

	console.log('\n\n>> PATHNAME : ');
	console.log(request.resourceURL.pathname);

	console.log('\n\n>> WSSERVER OBJECT : ');
	console.log(wsServer);

	console.log('\n\n>> CONNECTION STATE : ');
	console.log(connection.state);

	console.log("\n\n>> QUERY STRING : ");
	console.log(request.resourceURL.query);

	console.log("\n\n>> GET PARAMETER['q'] : ");
	console.log(request.resourceURL.query.q);

	console.log('\n\n>> CONNECTION : ');
	console.log(connection);

	connection.on('message', function(message) {
		console.log('Received Message :', message.utf8Data);
		connection.sendUTF('Hi this is WebSocket server!');
		connection.close();
	});
	
	connection.on('close', function(reasonCode, description) {
		console.log('Client has disconnected - Reason Code : ' + reasonCode + ' - Description : ' + description);
	});
});


websocket-client.html

<!--
	JavaScript WebSocket Client
	---------------------------------------------

	Reference :
		- https://www.pubnub.com/blog/nodejs-websocket-programming-examples/
		- https://www.npmjs.com/package/websocket
-->

<!DOCTYPE html>
<html>
<head>
	<title>WebSocket Playground</title>
</head>
<body>
</body>
</html>

<script type="text/javascript">

const ws = new WebSocket('ws://localhost:9898/farizfadian?q=abc');

ws.onopen = function() {
	console.log('WebSocket Client Connected');
	ws.send('Hi this is web client.');
};

ws.onmessage = function(e) {
	console.log("Received : '" + e.data + "'");
};

ws.onclose = function() {
	console.log('Disconnected');
};

</script>


Reference :


Friday, January 10, 2020

Install Java on Ubuntu

Java is a programming language used to build Android apps and real life web applications like Gmail and Google Docs. This ubiquitous language can be installed onto an Ubuntu server and its what we’ll be teaching in this tutorial today.

Pre-flight

  • Open the terminal and log in as root. If you are logged in as another user, you will need to add sudo before each command.
  • Working on a Linux Ubuntu 18.04 server
  • No installations of previous Java versions

Install Java

Step 1: Updating your system

apt-get update && apt-get upgrade

Step 2: Install the Repo

First, install the repository, its provided by a third party and you’ll need to press enter to continue downloading. The Oracle Java (JDK) Installer automatically downloads and installs Oracle JDK8.
add-apt-repository ppa:webupd8team/java
Update your system once more
apt-get update

Step 3: Install Java 8

Using the command below will kickstart the Java Installer which will assist in our task. Press Y when prompted to continue.
sudo apt install openjdk-8-jdk

Step 4: Verify the Installation of Java 8

java -version
Example Output:
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)

Set Java’s Home Environment

You may be looking to set Java’s home environment once we’ve installed it onto our server. To do that, lets first, let’s find Java’s path.
update-alternatives --config java
There is 1 choice for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/lib/jvm/java-8-oracle/jre/bin/java 1081 auto mode
* 1 /usr/lib/jvm/java-8-oracle/jre/bin/java 1081 manual mode
Take the highlighted path and add the variable into your /etc/environment file. Add the JAVA_HOME path after any line that is already present.
vim /etc/environment
Once you are in the file type `i` to insert the needed line:
JAVA_HOME="/usr/lib/jvm/java-8-oracle/jre/bin/java"
Save and exit the file by typing `ESC` followed by `:wq` and press `Enter`. Afterward, use the source command to have the system recognize the changes in your file.
source /etc/environment
You’ll know the variable has been set correctly by calling the variable.
echo $JAVA_HOME
Example Output:
root@1804:~$ echo $JAVA_HOME
/usr/lib/jvm/java-8-oracle/jre/bin/java

Reference:




Sunday, December 15, 2019

Install Java on CentOS

In this tutorial, we’ll be showing you how to install Oracle’s Java 8 programming language specifically onto a CentOS 7 server. This simple object-oriented language is used for many of the applications and websites you come across today.  Let’s jump right in!

Pre-flight

  1. Open the terminal and login as root.  If you are logged in as another user, you will need to add sudo before each command.
  2. Working on a Linux CentOS 7 server
  3. No installations of previous Java versions

Installing Java 8 on CentOS 7

Step 1: Update

As a matter of best practice we’ll update before installing any new programs:
yum -y update

Step 2: Install Java 8

yum install java-1.8.0-openjdk

Step 3:  Verify Java is Installed

java -version
Example Output:
java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)

Set Java’s Home Environment

Step 1: Find Java’s Path

Let’s set the JAVA_HOME variable, using the following command will give us a path so we can set the variable.
update-alternatives --config java
You’ll see a prompt to “Enter to keep the current selection[+], or type selection number:”, if you had multiple Java version you could set the default here, but all we need is the path of Java so we can exit pressing enter.  The highlighted area is the path we will need to copy/paste into our .bash_profile file.
There is 1 choice for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/lib/jvm/java-8-oracle/jre/bin/java 1081 auto mode
* 1 /usr/lib/jvm/java-8-oracle/jre/bin/java 1081 manual mode

Step 2: Setting Java’s Path in Your Environment

After copying your Java’s path, open the .bash_profile with your text editor.
vim .bash_profile
Export your Java path into the .bash_profile by adding the following to the bottom of the file. (Your path may look different from mine, and it’s not important that they vary.)
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64/jre/bin/java
Refresh the File:
source .bash_profile
When you use the JAVA_HOME variable you’ll now be able to see the path you set.
echo $JAVA_HOME
Example Output:
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64/jre/bin/java

Reference:

Saturday, August 27, 2011

How to Build Mail Server with Mercury Mail





Before you create a webmail server, download XAMPP here and download SquirrelMail here

1. Setting Mail Server
-Run Apache, MySQL, and MercuryMail in your PC Server

Image 1.1 Run this application.


-Click Configuration and then Manage core module..

Image 1.2 Configuration then Manage core module


-Click Local Domains Tab, and then Add your webmail domain

Image 1.3 In local domain then Add New Domain

-Create Localhost Or Server and Internet Name

Image 1.4 Create localhost server and internet name


-This is a preview your local domains

Image 1.5 Preview local domain


-And then, Click Configuration -> Manage Local User

Image 1.6 Configuration then Manage local user


-This is a preview User Definied for this System, And then Click Add to Create your Account Mail

Image 1.7 Create Account


-Create your Account Mail in Server

Image 1.8 Create Account


-This is a preview your Account Mail

Image 1.9 Preview your Account



2. Setting SquirrelMail

- Extract SquirrelMail in folder C:\xampp\htdocs\your_folder

Image 2.1 Extract SquirrelMail to htdocs


- Open htdocs/webmail/config /config_default.php

-Edit domain in line 118
 $domain = 'farislash.com';

-Edit use Send Mail in line 140
$useSendmail = 'false';

-Edit send mail path in line 180
$sendmail_path = 'C:/xampp/MercuryMail';

-Edit data directory in line 499
$data_dir = 'C:/xampp/MercuryMail/MAIL';

-Edit attachment directory in line
$attachment_dir = 'C:/xampp/MercuryMail/MAIL';
 
Save as file config_default.php -> config.php

-Finish
Run the webmail server in URL : http://localhost/webmail

Image 2.2 Login Mail


-This Web Mail is ready to use
Image 2.3 Webmail ready to use
 

Or http://your_ip_server:port/directory (example: http:192.168.1.1/webmail)