i am unclear of how to use socket.io-emitter (server to server) - socket.io

Below is how i initialize and emit but i dont find any errors nor do my server listening, receives any events.
const io = require('socket.io-emitter')({
host: 127.0.0.1,
// path: do i need this ?
port: redisUri.port,
pub: pub, // is the key required to be the same as the app ?
sub: sub, // is the key required to be the same as the app ?
});
io.emit('test', 'test'})
i tried many variations, can someone enlighten me how can i debug this ?
ive tried to add a redis href before the object as well.

I found the error, the issue was with the pub and sub sockets.
Here's the solution:
var pub = redisPassword ? redis.createClient(6379, localhost, {return_buffers: true, auth_pass: redisPassword}) : undefined;
var sub = redisPassword ? redis.createClient(6379, localhost, {detect_buffers: true, return_buffers: true, auth_pass: redisPassword}) : undefined;
socketio.adapter(require('socket.io-redis')({ host: localhost, port: 6379, pubClient: pub, subClient: sub }));
This was the issue:
Instead of having it at undefined for the checking of password, I
created another client, it was easier to set it to default.
Please do let me know if I could improve my code. Thank you.

Related

Using Express Router to proxy non-CORS content doubles request path

There must be something obvious I am missing. I have an external record like https/udspace.udel.edu/rest/handle/19716/28599 that is not set up for CORS, but I would like to get access to it in my application. In the past, I have set up a server to simply GET the resource and redeliver it for my application. However, I read about Node http-proxy-middleware and thought I'd try out a more direct proxy.
First, I could not use the target in the createProxyMiddleware() constructor because I wanted to send in the hostname and path of the desired resource like, perhaps, http://example.com/https/udspace.udel.edu/rest/handle/19716/28599 to get the resource above.
Relevant Code (index.js)
const express = require('express')
const { createProxyMiddleware } = require('http-proxy-middleware')
const app = express()
app.get('/info', (req, res, next) => {
res.send('Proxy is up.');
})
// Proxy endpoint
app.use('/https', createProxyMiddleware({
target: "incoming",
router: req => {
const protocol = 'https'
const hostname = req.path.split("/https/")[1].split("/")[0]
const path = req.path.split(hostname)[1]
console.log(`returning: ${protocol}, ${hostname}, ${path}`)
return `${protocol}://${hostname}/${path}`
},
changeOrigin: true
}))
app.listen(PORT, HOST, () => {
console.log(`Starting Proxy at ${HOST}:${PORT}`);
})
I was getting a 404 from the DSpace server without other information, so I knew that the request was going through, but transformed incorrectly. I tried again with an item in an S3 bucket, since AWS gives better errors and saw that my path was being duplicated:
404 Not Found
Code: NoSuchKey
Message: The specified key does not exist.
Key: api/cookbook/recipe/0001-mvm-image/manifest.json/https/iiif.io/api/cookbook/recipe/0001-mvm-image/manifest.json
What dumb thing am I doing wrong? Is this not what this proxy is for and I need to do something else?

Get IP address using uWebSockets

Please, how can I get sender IP address using uWebSockets?
const wss = new uws.Server({
server: server,
perMessageDeflate: false
});
wss.on(`connection`, ws => {
ws.remoteAddress = ws.upgradeReq.connection.remoteAddress;
console.log(`user connected: `, ws.remoteAddress);
});
user connected is undefined
Please try this:
wss.on(`connection`, ws => {
ws.remoteAddress = ws._socket.remoteAddress;
console.log(`user connected: `, ws.remoteAddress);
});
You can also log ws._socket to know what it is.
ws._socket.remoteAddress should work , but if you ws server is behind a load balancer, you can get it via :
ws.upgradeReq.headers["x-forwarded-for"]

Cannot use socket.io with redis on Heroku

I am trying to use socket io on Heroku with RedisToGo. In local environment, everything is fine.
When I deploy my code to the Heroku, most of the time I am getting 400 Bad Request with following data from the browser:
{"code":1,"message":"Session ID unknown"}
My redis config is:
var url = "redis://redistogo:xxx#lab.redistogo.com:xxxx/";
var rtg = require("url").parse(url);
var pub = redis.createClient(rtg.port, rtg.hostname, {return_buffers: true});
var sub = redis.createClient(rtg.port, rtg.hostname, {return_buffers: true});
pub.auth(rtg.auth.split(":")[1]);
sub.auth(rtg.auth.split(":")[1]);
var redisOptions = {
pubClient: pub,
subClient: sub,
host: rtg.hostname,
port: rtg.port
};
io.adapter(ioredis(redisOptions));
What is the problem?
The problem was Heroku accepting only websocket transport. Setting transports to websocket both in server and clients solved the problem.
Enabling session affinity on the Heroku app should fix the Session ID unknown error, you can do this by running
heroku features:enable http-session-affinity

node.js server running but not loading

I've just installed node.js on my computer running Win7(64bit).
The problem is that when I run a simple hello-world application it is running (as confirmed by console.log() and me pushing the code to OpenShift where it works just fine) but when I try to load the page in localhost:1337 it just keeps on loading (eventually times out).
I've no idea what to check, since firewall is not blocking node and I'm not running anything that would block the port.
Here's the server code.
#!/bin/env node
// Include http module.
var http = require("http");
//Get the environment variables we need if on OpenShift
var ipaddr = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT || 1337;
http.createServer(function (request, response) {
request.on("end", function () {
response.writeHead(200, {
'Content-Type': 'text/plain'
});
response.end('Hello HTTP!');
});
}).listen(port, ipaddr);
console.log('It works');
console.log('IP : ' + ipaddr + '\nPort : ' + port);
Any help is appreciated, thank you.
edit
Here's a screenshot of commandline output.
http://i.stack.imgur.com/GGaLD.png
The node server is hanging as you need to always call response.end.
I believe that listening to the end event on the request is causing the timeout. If you remove it will work.

redisClient.set :- no update no error

redisClient.get('abc', function(err, abcValue){
console.log(abcValue);
abcValue = abcValue + 'id';
redisClient.set('abc', abcValue, function(err){
console.log('abc updated');
});
});
nested updation over network, prints 'abc updated', but actual value does not update in redis database.
Note:- the above code works on localhost, but update not showing on heroku-redistogo.
Edit:- I'm running code on localhost, with redis connected to Redistogo. Using the following code:-
Setting up of express session:-
app.use(express.session({
store: new RedisStore({
host: 'birdeye.redistogo.com',
port: 1384,
db: 'redistogo',
pass: '052940128c2f2452f73378588dd5fb129'
}),
secret: '1234567890QWERTY',
}));
I am also creating another redisClient using the following code:-
var redisClient = require('redis').createClient( 1384, 'birdeye.redistogo.com', {detect_buffers: true});
redisClient.auth('052940128c2f2452f73378588dd5fb129', function() {
console.log('Redis client connected');
});
Do you see abc updated inside the console when running this code on Heroku ? It seems to be a misconfiguration of Redis client.

Resources