How to add parameters to a FeathersJS socket connection - javascript

I'm developing an app that uses a FeathersJS server, and an Electron app that uses FeathersJS Socket.io client to connect to the server. I'd like to use channels in the Electron app to get notified when some data in the server has changed. The Electron app is an "autonomuos" app, so there isn't any user authentication or interaction. The Electron app will have a unique id that represents each machine it is running on.
Taking into account that there isn't any kind of authentication, how can I add the Electron app's unique id in the socket.io connection so that this id is received in the server and it can create the corresponding channel in the FeathersJS server?
Thank you!

How to pass information on the server from the Socket.io transport to Feathers through a Socket.io middleware is documented in the Socket.io transport API. There also is an example how to pass query parameters from the client to the server in the Socket.io documentation. Put together it looks like this on the client:
const socket = io('http://feathers-server.com', {
query: {
token: 'cde'
}
});
And like this on the server:
app.configure(socketio(function(io) {
io.use(function (socket, next) {
socket.feathers.token = socket.handshake.query.token;
next();
});
}));
socket.feathers is the exact same as the channel connection object so it will now have a token property you can use to join as a channel:
app.on('connection', connection => {
if (connection.token) {
app.channel(connection.token).join(connection);
}
});

Related

Can't connect to Azure Cache for Redis from Azure Web App

I have NodeJS Web App which trying to connect to the Azure Cache for Redis which is part of the same subscription.
const redis = require('redis')
const redisConnectionConfig = {
host: REDIS_HOST,
port: REDIS_PORT,
auth_pass: REDIS_PASSWORD
tls: { servername: REDIS_HOST }
}
...
redis.createClient(redisConnectionConfig)
I'm able to connect to Redis from my local machine after adding my IP to the Redis Firewall rules.
Also, I've added all 'Outbound IPs and Additional Outbound IP Addresses' from the Service App Properties.
I've tried even to allow access from all IPs
still not pass
But it is not connected and if I try to use Redis I receive the following connection error:
MaxRetriesPerRequestError: Reached the max retries per request limit (which is 20). Refer to "maxRetriesPerRequest" option for details.
Something similar solved for the VM here. But in the case of the App Service Azure managed that layer.
Looks like it's not connectivity issue.
Network part you always can check via WebApp->Console and use command
tccping redisservername:redisserverport
tcpping_example
Probably something with your redis cache size. What size are you using now?
You can find azure redis limits here

Can socket.io connect to ws://?

Right now I am setting the nodejs server to use webscoket to receive the data from other server
I have tried to use websocket(ws) in nodejs server, and it can connect to the other server
const WebSocket = require('ws');
var ws = new WebSocketClient();
ws.open("ws://ws.something/?token="+Token);
But when I try to connect using socket.io, the console does not display a debug message, and no data is received
var io = require('socket.io-client')
const socket = io.connect('ws://ws.something/?token='+Token);
socket.on("connection", function(mSocket){
console.log('debug message')
mSocket.on("message", function(myData){
console.log(myData)
});
});
socket.on('error', function (err) {
console.log(err);
});
Can socket.io connect to ws:// ? Or is there something wrong with my code? (This is the first time to use it)
No, it cannot.
Socket.IO is a layer on top of several transports, with Web Sockets being only one of them. Socket.IO clients can only connect to Socket.IO servers.
If you want to connect to your Web Socket server from a browser, use the browser's built-in Web Socket client:
const socket = new WebSocket('wss://example.com');
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket

Where do I connect my mongodb Atlas to my react app?

I am building a React app with node.js with mongodb Atlas database. I have created the mongodb Atlas cluster and need to connect it to the React app. The mongodb documentation says to use this code
var MongoClient = require('mongodb').MongoClient;
var uri = "mongodb+srv://kay:myRealPassword#cluster0.mongodb.net/test";
MongoClient.connect(uri, function(err, client) {
const collection = client.db("test").collection("devices");
// perform actions on the collection object
client.close();
});
But - I'm not exactly sure where in my React app it should go. I am using react-router-dom and most places I put it breaks the app. Any ideas?
This code is designed to be used on server side of your app. As You've mentioned You have Node.js server, so put it there and use database connection to serve your data to the React client app.
For example, You can send some data to client when your server receives HTTP request with some endpoint.
Here is nice tutorial for that :)

Given a server on Node. js, How do I listen to the calls sent to the server from WebSocket?

When I run the server, I wanted to be able to listen to the messages coming in to the server. However, the program/server is only set up to receive the calls without any notice
server.js
var express = require("express"),
program = require("program"),
app = express.createServer();
app.use(express["static"](__dirname + "/../"));
app.listen(5000);
//app.server to clients
program.init({
oscPort: xxxx,
oscHost: "xxx.xxx.xxx"
socketPort: app
});
You should add socket server because HTTP and Socket it's different protocols. There is a bunch of WebSocket servers like ws, socket.io so you could pick the one which is most suitable in your case. Here is an example how to use Socket.io with Express.

how to check if user is authenticated (logged in) in a servlet/jsp application from node js

In my servlet/jsp application, I have a username,password, user_role stored in a database and my app is running in port 8080. I have a node js application running in port 8081 to implement some real time application. I am using express and socket.io in my node js app. Both my applications are running in windows server 2008.
In my node js app, before running following code:
io.sockets.on('connection', function (socket) {
//check if user is logged in jsp app then do following
socket.emit('news', { hello: 'world' });
});
How can I achieve this or how can I access session of servlet/jsp from node js ? Please suggest me with solutions that works on windows platform only.
You would let your server/jsp application create an authentication token and then send this token to your socket.io server as the first client event.
That means that you can't authenticate the socket.io client upon connection. The socket.io client needs to send this token first. Your socket.io server checks this token and then sets a an authenticated flag on the clients connection object.
Flow:
Browser requests token from server/jsp
server/jsp checks if user is logged in, if so, creates a secure token with userId (using private key)
Browser connects to socket.io server
Browser sends token to socket.io server
socket.io server checks whether token is valid (checks using public key)
socket.io sets an authenticated flag on the connection object.
...
socket.io server only emits events to connections with the authenticated flag.

Categories