How to call a function when node server is up? - javascript

I want to call a function when the net.serverCreate is up. It need to call this function when the server is started and not when a new connection appears. Event 'listening' dont fire...When I start the server I want to check something in Mysql data-base... How to execute functions when server starts?
My app:
var server = net.createServer(function (socket) {
socket.on('listening', function () {
console.log("Console is listening! "); //Its dont log
});
socket.on('data', function (message) {
});
socket.on('end', function () {
socket.end();
});
});
server.listen(1337,function(){
console.log("Server starts succefully"); //Its working!
});

Event 'listening' dont fire
The callback to createServer is handed a client Socket. Client-side sockets don't listen for requests; they make requests. So a listening event is never emitted by Sockets
On the other hand, net.createServer() returns a server-side socket(similar to Java's ServerSocket)
This should work.
server.listen(1337,function(){
//mysql db query
});
The callback to listen is executed when the server is bound to the specified port, i.e., when the server starts listening. The last parameter to listen is added as a listener for the listening event.
Another way to monitor the server-start event is:
server.on('listening', function() {
//mysql db query
});
Listening event

Related

What is difference between socket.on and io.on?

Server side code:
io.on('connection',(socket)=>{
console.log('New WEBSocket Connection')
socket.emit('message',"welcome")
socket.broadcast.emit('message',"A new user joined")
socket.on('textMessage',(message,callback)=>{
const filter = new Filter();
if(filter.isProfane(message)){
return callback('Mind your languezz')
}
io.emit('message',message)
callback()
})
}
Client side code:
socket.emit('textMessage',message,(error)=>{
if(error){
return console.log(error)
}
console.log("Message Delivered")
})
My doubt is, on client side code, what if I used io.on instead of socket.on?
socket.on('textMessage',(message,callback)=>{............instead I did it like this:
io.on('textMessage',(message,callback)=>{.............
io.on listens to the server events. connection is an event on the server, when a socket is connected. And when that socket is connected, the callback function inside that is run.
socket.on listens to events on that connected socket. socket.on('textMessage' asks to do something when textMessage event is emitted on the socket, which you do from your client using socket.emit('textMessage'

Socket.io question - how does the client and server communicate

I just started on socket.io. I am going through the example mentioned in the web page. Specifically the server code below
io.on('connection', function(socket){
socket.on('nitrous', function(msg){
io.emit('nitrous', msg);
console.log( "Server is emiting the event");
});
});
in conjunction with the client code below
<script src="/socket.io/socket.io.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function () {
var socket = io();
$('form').submit(function(e){
e.preventDefault(); // prevents page reloading
socket.emit('nitrous', $('#m').val());
$('#m').val('');
return false;
});
socket.on('nitrous', function(msg){
$('#messages').append($('<li>').text(msg));
console.log( "Client is emiting the event");
});
});
</script>
I understand that we the form is submitted, it would emit an event called 'nitrous' and then the handler registered on the socket would be invoked. But I also noticed that the handler on the socket object at the server too is getting invoked. My first question is how is this happening for all the users who are connected to the application. Secondly, in the server, there is the io.emit() with the same event name which is being passed - how and where is this handled ?. I did not include any io.on() but it still works.
I am referring to this example: https://socket.io/get-started/chat/#Integrating-Socket-IO
Please share your thoughts on the same.
Thanks,
Pavan.
let me rewrite your code to explain what is going on:
// Server
// when server gets new client connected do this:
serverSocket.on('connection', function (client) {
// when server receives event from client that called "nitrous" do this:
client.on('nitrous', function (msg) {
// emit event with name "nitrous" to every client - including sender
serverSocket.emit('nitrous', msg)
// or could just emit event to everyone but sender
// client.broadcast.emit('nitrous') // uncomment this line
console.log('Server receives the event from client')
})
})
Now client side (only javascript):
// Client
$(function () {
const clientSocket = io()
$('form').submit(function (e) {
e.preventDefault() // prevents page reloading
// message that client wants to send
const msg = $('#m').val()
// emit event called "nitrous" to server
clientSocket.emit('nitrous', msg)
// clear input field
$('#m').val('')
return false
});
// when client receives event from server called 'nitrous' do this:
clientSocket.on('nitrous', function (msg) {
// append LI element to list of messages
$('#messages').append($('<li>').text(msg))
console.log('Client receives the event')
})
})
Hope that makes more sense.

What is the purpose of custom eventemitters in node.js? [duplicate]

I am new to Node, and I am struggling to understand the main difference between Events and Functions. Both need to be triggered, so why do we need an Event at all if we have to trigger it anyway?
How is it different than having a Function triggered?
Example code:
var events = require('events');
var eventEmitter = new events.EventEmitter();
eventEmitter.on('event1', function () {
console.log('Event 1 executed.');
eventEmitter.emit('event2');
});
eventEmitter.on('event2', function() {
console.log('Event 2 executed.');
});
eventEmitter.emit('event1');
console.log('Program Ended.');
We can achieve the same result by functions, right?
I am sure this has some serious importance in Node (otherwise it would not exist, lol), but I am struggling to understand it.
Help appreciated! :)
Events deal with asynchronous operations. They aren't really related to functions in the sense that they are interchangeable.
eventEmitter.on is itself a function, it takes two arguments the event name, then a function (callback) to be executed when the event happens.
eventEmitter.on(evt, callback)
There is no way to tell WHEN the event will be emitted, so you provide a callback to be executed when the event occurs.
In your examples, you are controlling when the events are triggered, which is different than real world use where you may have a server listening for connections that could connect at anytime.
server.listen('9000', function(){
console.log('Server started');
});
server.on('connection', function(client){
console.log('New client connected');
doSomethingWithClient(client);
});
//series of synchronous events
function doSomethingWithClient(client){
//something with client
}
For server.listen the server doesn't start immediately, once its ready the callback is called
server.on('connection') listens for client connections, they can come at any time. The event is then triggered when a connection occurs, causing the callback to be run.
Then there is doSomethingWithClient this is just a function with a set of synchronous operations to be done when a client connection occurs.
Events useful in webserver code (that is active on port) not in normal scripts, in normal scripts events will behave same as functions because events will be continually active & listening on port for requests as long as port is up so if we use function instead, function will run only once when .js file is ran thus functions cannot capture incoming request and respond.
Example
In this code if you see output of below function dummy_func() triggered immediately when js file is ran & printed statement ConditionReport: A client Connected: 0 only once as output,
but the EventReport: A client Connected: printed only when opened http://localhost:58080 in browser and again in another tab if I open same http://localhost:58080 it printed again EventReport: A client Connected: 3
const express = require('express');
const app = express();
const path = require('path');
const PORT = process.env.PORT || 58080;
// Load all static html files in the directory, here index.html file open as default at http://localhost:58080/ but to load html like Resume.html we should call complete http://localhost:58080/Resume.html
app.use(express.static(path.join(__dirname)));
// Configure Port
var server_object=app.listen(PORT, () => console.log("Server listening on port " + PORT));
//Possible incoming Events
var incoming_events=server_object.eventNames()
console.log("\nserver_object:", incoming_events);
//On Event
var i=0
server_object.on('connection',()=>{
i=i+1
console.log("\nEventReport: A client Connected:",i);
});
//Using if condition instead of Event
function dummy_func(j){
console.log("\nConditionReport: A client Connected:",j,"\n");
}
var j=0
if (incoming_events.includes('connection')){
dummy_func(j)
j=j+1
}
OUTPUT:
stataraju#statara-ltm7wjr Example2 % node index.js
server_object: [ 'request', 'connection', 'listening' ]
ConditionReport: A client Connected: 0
Server listening on port 58080
EventReport: A client Connected: 1
EventReport: A client Connected: 2
EventReport: A client Connected: 3
I suppose I see the biggest difference I see is that an event emitter could trigger multiple events that are listening, whereas just calling a function only triggers one thing.
So for example, you could have numerous objects in a game that are all waiting for a step event that increments their animation.
They are such a pain to debug though that I'd much rather just use functions.
An event is an identifier used within tools (.on(), .emit() etc) to set and execute callbacks. Functions are reusable code.

Why does my socket.io app disconnect immediately when run on Raspberry pi?

I am attempting to build a node.js application on a Raspberry Pi which can communicate with a remote socket.io server.
The socket.io server quite simply writes to the console when a new connection is established and when an existing connection is closed.
The socket.io client works as expected when run in a browser.
However, when I run the client code from a Raspberry Pi it connects and immediately terminates. This does not allow me to perform any function such as emitting or receiving.
The on connect event is never fired.
var io = require('/usr/local/lib/node_modules/socket.io-client');
var serverUrl = 'http://remoteserver.com';
console.log('Connecting to ' + serverUrl);
var socket = io.connect(serverUrl);
socket.on('connect', function(socket) {
console.log('Connection established!');
console.log(socket.id);
socket.on('disconnect', function() {
console.log('Disconnected from server');
});
});
The code above will output:
Connecting to: http://remoteserver.com
On the server side, the output will show a new connection and eventually a timeout close.
Why is the client on connect event not firing? How can I persist this connection so that I can eventually send inputs to the server?
Remove the line:
socket.close();
from your code. You are closing the socket immediately. Remember that all the event handlers are asynchronous. They are non-blocking and will be called some time in the future. Meanwhile, the rest of your code runs to completion and thus your socket.close() is executed before your client has any chance to get any events (other than the disconnect event).
If you want to do some stuff and then close the socket, then you need to close the socket only from some event handler that indicates that your operation is complete and you are now done with the socket.
You only need to call socket.close() when you want to close the connection from the raspberry pi. Also, the socket.io-client documentation does not use the .connect method, but instead calls require('socket.io-client')(serverUrl)
var io = require('/usr/local/lib/node_modules/socket.io-client');
var serverUrl = 'http://remoteserver.com';
console.log('Connecting to ' + serverUrl);
var socket = io(serverUrl);
socket.on('connect', function(socket) {
console.log('Connection established!');
console.log(socket.id);
});
socket.on('disconnect', function() {
console.log('Disconnected from server');
});
//Removed the socket.close() line

Difference between Events and Functions?

I am new to Node, and I am struggling to understand the main difference between Events and Functions. Both need to be triggered, so why do we need an Event at all if we have to trigger it anyway?
How is it different than having a Function triggered?
Example code:
var events = require('events');
var eventEmitter = new events.EventEmitter();
eventEmitter.on('event1', function () {
console.log('Event 1 executed.');
eventEmitter.emit('event2');
});
eventEmitter.on('event2', function() {
console.log('Event 2 executed.');
});
eventEmitter.emit('event1');
console.log('Program Ended.');
We can achieve the same result by functions, right?
I am sure this has some serious importance in Node (otherwise it would not exist, lol), but I am struggling to understand it.
Help appreciated! :)
Events deal with asynchronous operations. They aren't really related to functions in the sense that they are interchangeable.
eventEmitter.on is itself a function, it takes two arguments the event name, then a function (callback) to be executed when the event happens.
eventEmitter.on(evt, callback)
There is no way to tell WHEN the event will be emitted, so you provide a callback to be executed when the event occurs.
In your examples, you are controlling when the events are triggered, which is different than real world use where you may have a server listening for connections that could connect at anytime.
server.listen('9000', function(){
console.log('Server started');
});
server.on('connection', function(client){
console.log('New client connected');
doSomethingWithClient(client);
});
//series of synchronous events
function doSomethingWithClient(client){
//something with client
}
For server.listen the server doesn't start immediately, once its ready the callback is called
server.on('connection') listens for client connections, they can come at any time. The event is then triggered when a connection occurs, causing the callback to be run.
Then there is doSomethingWithClient this is just a function with a set of synchronous operations to be done when a client connection occurs.
Events useful in webserver code (that is active on port) not in normal scripts, in normal scripts events will behave same as functions because events will be continually active & listening on port for requests as long as port is up so if we use function instead, function will run only once when .js file is ran thus functions cannot capture incoming request and respond.
Example
In this code if you see output of below function dummy_func() triggered immediately when js file is ran & printed statement ConditionReport: A client Connected: 0 only once as output,
but the EventReport: A client Connected: printed only when opened http://localhost:58080 in browser and again in another tab if I open same http://localhost:58080 it printed again EventReport: A client Connected: 3
const express = require('express');
const app = express();
const path = require('path');
const PORT = process.env.PORT || 58080;
// Load all static html files in the directory, here index.html file open as default at http://localhost:58080/ but to load html like Resume.html we should call complete http://localhost:58080/Resume.html
app.use(express.static(path.join(__dirname)));
// Configure Port
var server_object=app.listen(PORT, () => console.log("Server listening on port " + PORT));
//Possible incoming Events
var incoming_events=server_object.eventNames()
console.log("\nserver_object:", incoming_events);
//On Event
var i=0
server_object.on('connection',()=>{
i=i+1
console.log("\nEventReport: A client Connected:",i);
});
//Using if condition instead of Event
function dummy_func(j){
console.log("\nConditionReport: A client Connected:",j,"\n");
}
var j=0
if (incoming_events.includes('connection')){
dummy_func(j)
j=j+1
}
OUTPUT:
stataraju#statara-ltm7wjr Example2 % node index.js
server_object: [ 'request', 'connection', 'listening' ]
ConditionReport: A client Connected: 0
Server listening on port 58080
EventReport: A client Connected: 1
EventReport: A client Connected: 2
EventReport: A client Connected: 3
I suppose I see the biggest difference I see is that an event emitter could trigger multiple events that are listening, whereas just calling a function only triggers one thing.
So for example, you could have numerous objects in a game that are all waiting for a step event that increments their animation.
They are such a pain to debug though that I'd much rather just use functions.
An event is an identifier used within tools (.on(), .emit() etc) to set and execute callbacks. Functions are reusable code.

Categories

Resources