redis pubsub in nodejs with socketio: wht doesn't publisher publish message? - javascript

I'm trying pubsub with redis and socketio in nodejs.
My server-side code is:
var io = require('socket.io').listen(server);
var pub = redis.createClient();
io.sockets.on("connection", function(socket) {
console.log('connecteed');
var sub = redis.createClient();
sub.subscribe("messages");
sub.on("message", function(channel, message) {
console.log('message',message);
socket.emit(channel,message);
});
socket.on("disconnect", function() {
sub.unsubscribe("messages");
sub.quit();
});
});
pub.publish("messages", JSON.stringify({type: "foo", content: "bar"}));
My html page index.html contains the following script:
var host = window.location.host.split(':')[0];
var socket = io.connect('http://' + host);
socket.on('messages',function(msg){
console.log(msg);
})
but in index.html console.log is never executed.
It is elementary, but i don't find the error in my code. Where is it?

That probably because you don't have any clients connected when you publish the message (hint io.socket.on connect function is executed after pub.publish). Try to replace
pub.publish("messages", JSON.stringify({type: "foo", content: "bar"}));
with
setIntervall(function(){
pub.publish("messages", JSON.stringify({type: "foo", content: "bar"}));
},1000);
This will send the message each second and will give you plenty of time to verify your setup.

Related

SO_REUSEADDR in NodeJs using net package

I have two backends. Backend A and Backend B.
Backend B sends and receives info using a socket server running at port 4243.
Then, with Backend A, I need to catch that info and save it. But I have to also have a socket server on Backend A running at port 4243.
The problem is that, when I run Backend A after running Backend B I receive the error "EADDRINUSE", because I'm using the same host:port on both apps.
If, for Backend A I use Python, the problem dissapear because I have a configuration for sockets that's called SO_REUSEADDR.
Here we have some examples:
https://www.programcreek.com/python/example/410/socket.SO_REUSEADDR
https://subscription.packtpub.com/book/networking-and-servers/9781849513463/1/ch01lvl1sec18/reusing-socket-addresses
But, I want to use JavaScript for coding my Backend A, so I was using the net package for coding the sockets, and I can't get it to work, because of the "EADDRINUSE" error.
The NodeJS documentation says that "All sockets in Node set SO_REUSEADDR already", but it doesn't seem to work for me...
This is my code so far:
// Step 0: Create the netServer and the netClient
console.log(`[DEBUG] Server will listen to: ${HOST}:${PORT}`);
console.log(`[DEBUG] Server will register with: ${AGENT_ID}`);
const netServer = net.createServer((c) => {
console.log('[netServer] Client connected');
c.on('message', (msg) => {
console.log('[netServer] Received `message`, MSG:', msg.toString());
});
c.on('*', (event, msg) => {
console.log('[netServer] Received `*`, EVENT:', event);
console.log('[netServer] Received `*`, MSG:', msg);
});
}).listen({
host: HOST, // 'localhost',
port: PORT, // 4243,
family: 4, // ipv4, same as socket.AF_INET for python
});
// Code copied from nodejs documentation page (doesn't make any difference)
netServer.on('error', function (e) {
if (e.code == 'EADDRINUSE') {
console.log('Address in use, retrying...');
setTimeout(function () {
netServer.close();
netServer.listen(PORT, HOST);
}, 1000);
}
});
const netClient = net.createConnection(PORT, HOST, () => {
console.log('[netClient] Connected');
});
// Step 1: Register to instance B of DTN with agent ID 'bundlesink'
netClient.write(serializeMessage({
messageType: AAPMessageTypes.REGISTER,
eid: AGENT_ID,
}));
With this code, I get the following output in the terminal:
But, with the Python code, the socket connects successfully:
I don't know what to do :(
I hope I get some help here.
Edit 1
By the way, the lsof command, throws me this output for the JavaScript backend:
And this other output for the Python backend:
Edit 2
It really seems to be a problem with JavaScript. I also found this snippet:
var net = require('net');
function startServer(port, host, callback) {
var server = net.createServer();
server.listen(port, host, function() {
callback(undefined, server);
});
server.on('error', function(error) {
console.error('Ah damn!', error);
callback(error);
});
}
startServer(4000, '0.0.0.0', function(error, wildcardServer) {
if (error) return;
startServer(4000, '127.0.0.1', function(error, localhostServer) {
if (error) return;
console.log('Started both servers!');
});
});
From this post:
https://medium.com/#eplawless/node-js-is-a-liar-sometimes-8a28196d56b6
As the author says:
Well, that prints “Started both servers!” which is exactly what we don’t want.
But for me, instead of printing that, I get an error:
Ah damn! Error: listen EADDRINUSE: address already in use 127.0.0.1:4000
at Server.setupListenHandle [as _listen2] (node:net:1319:16)
at listenInCluster (node:net:1367:12)
at doListen (node:net:1505:7)
at processTicksAndRejections (node:internal/process/task_queues:84:21) {
code: 'EADDRINUSE',
errno: -98,
syscall: 'listen',
address: '127.0.0.1',
port: 4000
}
I really cannot make it to run and print "Started both servers!".
Because that's what I want my code to do.
Edit 3
This is the Python server socket: https://gitlab.com/d3tn/ud3tn/-/blob/master/tools/aap/aap_receive.py
This is the important part:
addr = (args.tcp[0], int(args.tcp[1])) # args.tcp[0] = "localhost", args.tcp[1] = "4243"
with AAPTCPClient(address=addr) as aap_client:
aap_client.register(args.agentid) # args.agentid = "bundlesink"
run_aap_recv(aap_client, args.count, args.verify_pl)
It creates an AAPTCPClient, and the only thing that AAPTCPClient does, is the following:
def __init__(self, socket, address):
self.socket = socket
self.address = address
self.node_eid = None
self.agent_id = None
def register(self, agent_id=None):
"""Attempt to register the specified agent identifier.
Args:
agent_id: The agent identifier to be registered. If None,
uuid.uuid4() is called to generate one.
"""
self.agent_id = agent_id or str(uuid.uuid4())
logger.info(f"Sending REGISTER message for '{agent_id}'...")
msg_ack = self.send(
AAPMessage(AAPMessageType.REGISTER, self.agent_id)
)
assert msg_ack.msg_type == AAPMessageType.ACK
logger.info("ACK message received!")
def send(self, aap_msg):
"""Serialize and send the provided `AAPMessage` to the AAP endpoint.
Args:
aap_msg: The `AAPMessage` to be sent.
"""
self.socket.send(aap_msg.serialize())
return self.receive()
def receive(self):
"""Receive and return the next `AAPMessage`."""
buf = bytearray()
msg = None
while msg is None:
data = self.socket.recv(1)
if not data:
logger.info("Disconnected")
return None
buf += data
try:
msg = AAPMessage.parse(buf)
except InsufficientAAPDataError:
continue
return msg
I don't see any bind, and I don't understand why the python code can call "socket.recv", but in my JavaScript code I can't do "netServer.listen". I think it should be the same.
There are things to clarify.
1.) The client uses the bind syscall where the kernel selects the source port automatically.
It does so by checking sys local_portrange sysctl settings.
1.) If you want to bind the client to a static source port, be sure to select a TCP port outside the local_portrange range !
2.) You cannot subscribe to event "*", instead you've to subscribe to the event "data" to receive messages.
For best practice you should also subscribe to the "error" event in case of errors !
These links will get you started right away:
How do SO_REUSEADDR and SO_REUSEPORT differ?
https://idea.popcount.org/2014-04-03-bind-before-connect/
So, for all beginners, who want to dig deeper into networking using node.js…
A working server example:
// Step 0: Create the netServer and the netClient
//
var HOST = 'localhost';
var PORT = 4243;
var AGENT_ID = 'SO_REUSEADDR DEMO';
var net = require('net');
console.log(`[DEBUG] Server will listen to: ${HOST}:${PORT}`);
console.log(`[DEBUG] Server will register with: ${AGENT_ID}`);
const netServer = net.createServer((c) => {
console.log('[netServer] Client connected');
c.on('data', (msg) => {
console.log('[netServer] Received `message`, MSG:', msg.toString());
});
c.on('end', () => {
console.log('client disconnected');
});
c.on('error', function (e) {
console.log('Error: ' + e.code);
});
c.write('hello\r\n');
c.pipe(c);
}).listen({
host: HOST,
port: PORT,
family: 4, // ipv4, same as socket.AF_INET for python
});
// Code copied from nodejs documentation page (doesn't make any difference)
netServer.on('error', function (e) {
console.log('Error: ' + e.code);
if (e.code == 'EADDRINUSE') {
console.log('Address in use, retrying...');
setTimeout(function () {
netServer.close();
netServer.listen(HOST, PORT);
}, 1000);
}
if ( e.code = 'ECONNRESET' ){
console.log('Connection reset by peer...');
setTimeout(function () {
netServer.close();
netServer.listen(HOST, PORT);
}, 1000);
}
});
The Client:
/* Or use this example tcp client written in node.js. (Originated with
example code from
http://www.hacksparrow.com/tcp-socket-programming-in-node-js.html.) */
var net = require('net');
var HOST = 'localhost';
var PORT = 4243;
var client = new net.Socket();
client.setTimeout(3000);
client.connect(PORT, HOST, function() {
console.log("Connected to " + client.address().address + " Source Port: " + client.address().port + " Family: " + client.address().family);
client.write('Hello, server! Love, Client.');
});
client.on('data', function(data) {
console.log('Received: ' + data);
client.end();
});
client.on('error', function(e) {
console.log('Error: ' + e.code);
});
client.on('timeout', () => {
console.log('socket timeout');
client.end();
});
client.on('close', function() {
console.log('Connection closed');
});
Best Hannes
Steffen Ullrich was completely right.
In my JavaScript code, I was trying to create a server to listen to the port 4243.
But you don't need to have a server in order to listen to some port, you can listen with a client too! (At least that's what I understood)
You can create a client connection as following:
const netClient = net.createConnection(PORT, HOST, () => {
console.log('[netClient] Connected');
});
netClient.on('data', (data) => {
console.log('[netClient] Received data:', data.toString('utf8'));
});
And with "client.on", then you can receive messages as well, as if it were a server.
I hope this is useful to someone else.

NodeJS, websocket, and module.export

I have a server.js file.
Inside, i defined my routes.
// routes
var mainRoutes = require('./routes/main.js')(app, express);
var apiRoutes = require('./routes/api.js')(app, express);
var socketRoutes = require('./routes/socket.js');
app.use('/', mainRoutes);
app.use('/api', apiRoutes);
// socket.io communication
io.sockets.on('connection', socketRoutes);
My socket.js file looks like that :
module.exports = function (socket) {
console.log('user connected');
socket.on('myEvent', function(data) {
// whatever ...
});
}
Inside this function, i can catch events and send some.
BUT i need to send a message to everyone at some point. Let say when i receive an 'myEvent' event.
So basically, i would like to do :
io.sockets.emit('messageForEveryone', "This is a test");
But here, i can only work on the 'socket' argument, which is for 1 person only i guess.
I would like to pass io from server.js, to socket.js.
I tried that (in server.js) :
var socketRoutes = require('./routes/socket.js', io);
And that (in socket.js) :
module.exports = function (io, socket)
Obviously, it's not working. I don't even understand where the socket argument is coming from.
Question : How can i work on io object, when i'm inside the module.export of the sockets.js file ?
I would really appreciate any help, i'm new to all of this.
Thanks !
Since you just want to emit to all clients, instead of passing io to socketRoutes, you can simply do this.
module.exports = function (socket) {
var sockets = this;
console.log('user connected');
socket.on('myEvent', function(data) {
sockets.emit('hello_all_clients',data);
});
}
You could return a function from exports as such.
module.exports = function (io) { // pass io to initialize
return function (socket) {
// io and socket are both scoped
}
}
then in server.js
// socket.io communication
io.sockets.on('connection', socketRoutes(io));

Sails.js 0.10 sockets not connecting

With the out of date docs on sails' sockets implementation i'm struggling to get the basic 'connect' message. Here's my node.js server side code:
sails.sockets.on('connection', function(socket){
sails.log.info('socket connected');
//create room and broadcast a welcome message
socket.emit('user joined', {'message': 'Welcome to ' + roomName});
socket.join(roomName);
socket.broadcast.to(roomName).emit('user joined', {'message': 'Welcome to ' + roomName});
});
and my client side:
var sock = io.connect('http://localhost:8888');
sock.on('connection', function(socket){
console.log('conected to server');
});
sock.on('user joined', function (json) {
console.log('socket: ' + json);
});
I do get the OK message from sails itself on start up but can't seem to get a connection of my own:
sails.io.js:200 `io.socket` connected successfully.
(for help, see: http://sailsjs.org/#!documentation/reference/BrowserSDK/BrowserSDK.html)
I tried using socket.io v1 and got the same weird situation. any ideas? thanks!
On server config/sockets.js
module.exports.sockets = {
onConnect: function(session, socket) {
sails.log.verbose('>>> socket user connected');
sails.sockets.blast('eventName', dataToBlast);
},
};
Documentation http://sailsjs.org/#/documentation/anatomy/myApp/config/sockets.js.html
On client
io.socket.on('eventName', function(dataToBlast) {
// process dataToBlast
});
Documentation http://sailsjs.org/#/documentation/reference/websockets/sails.io.js/io.socket.on.html
Be aware that socket needs to be subscribed to eventName. To subscribe you need to make controller SubscribeController.js and make request to it's action via socket.
var SubscribeController = {
sub: function(req, res) {
ModelName.subscribe(
req.socket,
[] /*records to subscribe to or empty array to subscribe to all */,
['eventName'] /* array of strings eventNames */
);
},
};
For debugging client you can connect to firehose which will give your client all messages from sails server
io.socket.get('/firehose');
io.socket.on('firehose', function newMessageFromSails(message) {
typeof console !== 'undefined' &&
console.log('New message published from Sails ::\n', message);
});

socket is not defined while running a server on Node Js

I am trying to create a chat application using node js and mongodb. I am looking on a tutorial for this. I couldn't solve a error that states socket is not defined while running my file server.js. The code in server Js is
var mongo = require('mongodb').MongoClient,
client = require('socket.io').listen(8080).sockets;
console.log(mongo);
mongo.connect('mongodb://#127.0.0.1/chat',function(err,db) {
if(err) throw err;
client.on('connection',function() {
//Wait for Input
socket.on('input',function(data) {
console.log(data);
});
});
});
The error is created when i wanted to listen socket on input.When i try to define socket as
socket =io.connect('http://127.0.0.1:8080'); It again gives error stating io not defined. Isn't io global on nodejs?
Please enlighten me on this.
Try as below if you are using express.
var express = require('express'),
, app = express()
, server = require('http').Server(app)
, mongo = require('mongodb').MongoClient,
, io = require('socket.io')(server);
server.listen(3000, function() {
console.log("Server Running on port 3000");
});
mongo.connect('mongodb://#127.0.0.1/chat',function(err,db) {
io.sockets.once('connection', function(socket) {
io.sockets.emit('new-data', {
channel: 'stdout',
value: "My Data"
});
});
});
In you view.html
<html>
<head>
<script src="https://cdn.socket.io/socket.io-1.0.6.js"></script>
var socket = io.connect('http://localhost');
var streamer = $('#streamer');
socket.on('new-data', function(data) {
streamer.val(data.value);
});
</head>
<body>
<div id="streamer"> </div>
</body>
</html>

Nodejs server unable to detect connection with Pubnub+SocketIO

My nodejs server is unable to detect when a new browser connects ('connection' event) and I dont know why. I narrowed down a problem working on it for a few days and suspect that is has to due with the addition of the pubnub socket connection implemented on the browser.
The following is my server.js
var http = require('http')
, connect = require('connect')
, io = require('socket.io')
, fs = require('fs')
, uuid = require('node-uuid')
, _ = require('lodash');
// pubnub!!! (how to initialize it for use on server)
var pubnub = require('pubnub').init({
channel: "my_channel",
publish_key: "pub-key",
subscribe_key: "sub-c-key",
uuid: "Server",
origin : 'pubsub.pubnub.com'
});
pubnub.subscribe({
channel: 'my_channel',
callback: function(message) {
console.log("Message received: ", message);
},
message: 'Server ready',
presence: function(data) {
console.log("Presense: ", data);
},
connect: publish
});
// various socket.on() events omitted
var app = connect().use(connect.static(__dirname)).use(connect.directory(__dirname));
var server = http.createServer(app);
server.listen(8888);
io = io.listen(server);
io.sockets.on('connection', handleNewPeer);
Upon arriving on the html page, the doConnect(isBroadcaster) function is ran from script tag
The doConnect function (In peer.js):
var doConnect = function(isBroadcaster) {
console.log("doConnect");
// broadcaster or normal peer
var user;
if (isBroadcaster)
user = "Broadcaster";
else
user = "Viewer";
(function() {
var pubnub_setup = {
channel: "my_channel",
publish_key: "pub-c-key",
subscribe_key: "sub-c-key",
user: user
};
// Note removed the var
socket = io.connect( 'http://pubsub.pubnub.com', pubnub_setup);
// various socket.on() omitted
})();
Here is what how it was before with just socketIO & it was working:
var doConnect = function(isBroadcaster) {
socket = io.connect();
// various socket.on() omitted
}
My p2p video website is implemented with WebRTC running on a Nodejs + SocketIO server.
I have been trying to incorporate pubnub into it & thought it would be easy since pubnub supports SocketIO (or at least client side?). Really did not think it would be this difficult to set up server side.
Any input at all on this? I think it's something simple that I just cannot put my finger on
Socket.IO on the Server using Node.JS
Socket.IO with PubNub does not provide a Node.JS Socket.IO backend option. However you can use the PubNub SDK directly for on-connect events.
NPM Package
npm install pubnub
After you install the PubNub NPM you can use the node.js server backend:
Node.js Backend Code
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// PubNub!!! (how to initialize it for use on server)
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
var pubnub = require('pubnub').init({
publish_key : "pub-key",
subscribe_key : "sub-c-key",
uuid : "Server-ID"
});
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// On user Connect
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
function on_user_connect(data) {
console.log( "User Connected: ", data.uuid );
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// On user Leave
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
function on_user_leave(data) {
console.log( "User Left: ", data.uuid );
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Open Socket Connection for User Join Events
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
pubnub.subscribe({
channel : 'my_channel',
connect : connected
message : function(message) {
console.log("Message received: ", message);
},
presence : function(data) {
if (data.action == "leave") on_user_leave(data);
if (data.action == "timeout") on_user_leave(data);
if (data.action == "join") on_user_connect(data);
}
});
function connected() {
console.log('connected!');
}
What version of socket.io are you using?
This might not fix it. I am using version 1.+ Have you tried:
io.on('connection', function(socket){
console.log('user connected');
});

Categories