Socket.IO enables real-time bidirectional event-based communication.
It works on every platform, browser or device, focusing equally on reliability and speed.
Node
======
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.
1. Install Node.js
(Choose recommended version)
https://nodejs.org/en/
2. Open cmd prompt from windows
and type npm
and open again cmd
Type cd c:\
then cd testapp
npm install socketio
then
npm install express
6. We will create package.json in testapp folder
type
npm init
I can edit or press enter for default values.
Then type yes
Create a file inside testapp
index.js
=======
var app = require('express')();
var server = app.listen(8080,'localhost');
var io = require('socket.io').listen(server);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
and html file
index.html
========
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://localhost:8080/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io.connect('http://localhost:8080');
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
</script>
</body>
</html>
Here socket.emit allows you to emit custom events on the server and client
-----------------------------------------------------------------------------
We are done.
Now in cmd prompt
type node index.js
do not close... it starts the server
now open html in browser
For more about socket.io
https://www.tutorialspoint.com/socket.io/index.html
About Express
http://expressjs.com/
It works on every platform, browser or device, focusing equally on reliability and speed.
Node
======
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.
1. Install Node.js
(Choose recommended version)
https://nodejs.org/en/
2. Open cmd prompt from windows
and type npm
3. Create a directory called as testapp in c drive
and open again cmd
Type cd c:\
then cd testapp
5. Now we will install express and socket io
then
npm install express
6. We will create package.json in testapp folder
type
npm init
I can edit or press enter for default values.
Then type yes
Create a file inside testapp
index.js
=======
var app = require('express')();
var server = app.listen(8080,'localhost');
var io = require('socket.io').listen(server);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
and html file
index.html
========
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://localhost:8080/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io.connect('http://localhost:8080');
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
</script>
</body>
</html>
Here socket.emit allows you to emit custom events on the server and client
-----------------------------------------------------------------------------
We are done.
Now in cmd prompt
type node index.js
do not close... it starts the server
now open html in browser
For more about socket.io
https://www.tutorialspoint.com/socket.io/index.html
About Express
http://expressjs.com/