Node.js - How to share sessions between http server and net server? - node.js

I'm trying to make server that helps http clients and net clients to connect between them, but I'm not sure that whether some module such as redis can help the server to share sessions or make connections from http to net.
Because I wanna use cluster (because of load balancing) to optimize speed in multi-core processors, I think the sessions must be shared.
Here is the server what I make

Using Redis is a good approach to save and share session details when using clusters. Read more on this approach here Nodejs Clustering and expressjs sessions.
Another approach would be to use Express Session module if your web application is developed using ExpressJS. This link https://medium.com/#karaxuna/how-to-share-session-across-mutiple-nodejs-http-server-instances-7d466389d123#.wrg84qel6 provides on how to configure Express Session module.

Related

Building scalable SPAs using websockets, how complex is it?

I am beginner in web technologies. While studying about frontend frameworks, I came to know that we run separate application servers for frontend and backend server(API).
e.g. If I am using vue.js, I'll be running a vue server for frontend and I'll be running another MEAN stack API server separately. Now my application will need to show real-time updates, I'll have to use websocket connection between my browser and frontend server which further will need websocket/webhook connection with my backend server(API). Now I am already aware of the scalability and session management issues with websocket connection. In this scenario, how should I build my application for better scalability and with less complexity? Is it possible to create a monolithic application server for frontend and backend? Will that be a good choice?
Is it possible to create a monolithic application server for frontend and backend? Will that be a good choice?
That choice is fine; start simple and you can break into microservices in the future. Unless this is for a large production system!
If you use something like express you can serve the Vue.js files using express.static, API endpoints using express.Router() instances, and the ws or socket.io module attached to express instance for websockets.
Now my application will need to show real-time updates, I'll have to
use websocket connection between my browser and frontend server which
further will need websocket/webhook connection with my backend server
This isn't the case. You can have your frontend (the app running in a browser) connect directly to the backend via websocket if you wish, no need to proxy via a frontend server. For session management look into JWT tokens.
You can go for socket.io library in Nodejs. It's simple and easy to use, The scalability and session can be handled by introducing Redis,
check https://socket.io/docs/using-multiple-nodes/

Loopback and Express wiring

I am planning to build a new application with express (for frontend) and loopback for managing all APIs, hosted on different servers.
How would you typically architect this, would the app (browser) directly make http requests to loopback for data, or would all requests go through expressjs and user never interacts with loopback?
If its the former, how do you do session management? If its latter, would you need to recreate all routes even in express?
Would appreciate some help.
Disclaimer: I am co-author and a core developer of LoopBack.
LoopBack is using Express under the hood. Every LoopBack application is an Express application too, therefore you can use any Express compatible middleware (like session management) and define Express-based routes in your LoopBack project.
It's entirely possible to write a LoopBack application that's serving both REST/JSON API and front-end files, we have users successfully running this setup in production.
As for session management, I don't know what exactly are you asking about. In general, you handle sessions in LoopBack the same way you handle them in Express.
You may find the following resources helpful:
Defining middleware
Use cookies securely

Multi threading nodeJS and Socket IO

Okay so multithreading nodeJS isn't much problem from what I've been reading. Just deploy several identical apps and use nginx as reverse proxy and load balancer of all the apps.
But actually native cluster module works pretty well too, I found.
However, what if I have socket.io with the nodeJS app? I have tried the same strategy with nodeJS + socket.IO; however, it obviously did not work because every socket event emitted will be more or less evenly distributed and sockets other than the one that made the connection would have no idea where the request came from.
So best method I can think of right now is to separate nodeJS server and socket.IO server. Scale nodeJS server horizontally (multiple identical apps) but just have one socket.IO server. Although I believe it would be enough for the purpose of our solution, I still need to look out for future. Has anyone succeeded in horizontally scaling Socket.IO? So multiple threads?
The guidelines on the socket.io website use Redis with a package called socket.io-redis
https://socket.io/docs/using-multiple-nodes/
Looks like is just acts like a single pool for the connections, and each node instance connects to that.
Putting your socket server on a separate service (micro-service) is a probably fine, the downside is needing to manage communications between the two instances.

How to direct a user to an available websocket server when she logs in to my multi-server Node.js app?

This is more like a design question but I have no idea where to start.
Suppose I have a realtime Node.js app that runs on multiple servers. When a user logs in she doesn't know which server she will be assigned to. She will just login, do something and logout and that's it. A user won't be interacting with other users on a different server, nor will her details be stored on another server.
In the backend I assume the Node.js server will put the user's login details to some queue and then when there is space it will assign this user to an available server (A server that has the lowest ping value or is not full). Because there is a limit number of users on one physical server when the users try to login to a "full" server it will direct her to another available server.
I am using ws module of node.js. Is there any service available for this purpose or do I have to build my own? How difficult would that be?
I am not sure how websocket fits into this question. Ignoring it. I guess your actual question is about load balancing... Let me try paraphasing it.
Q: Does NodeJS has any load balancing feature that I can leverage?
Yes and it is called cluster in NodeJS. Instead of the traditional one node process listening on a single port, this module allows you to spawn a group of node processes and have them all binded to the same port.
This means is that all the user know is only the service's endpoint. He sends a request to it and 1 of the available server in the group will serve him whenever possible.
Alternatively using Nginx, the web server, as your load balancer is also a very popular approach to this problem.
References:
Cluster API: https://nodejs.org/api/cluster.html
Nginx as load balancer: http://nginx.org/en/docs/http/load_balancing.html
P.S
I guess the key word for googling solutions to your problem is load balancer.
Out of the 2 solutions I would recommend going the Nginx way as it is a much scalable approach
Example:
Your Node process could possibly be spread across multiple hosts (horizontal scaling). The former solution is more for vertical scaling, taking advantages of multi-cores machine.

What's different between connect framework and socket.io?

I'm new to node.js, so i have some questions about connect framework and socket.io:
What's different? i'm confused about it.
Should i use connect fw with socket.io or just use socket.io?
The Connect module is a web application framework, while Socket.IO is a realtime transport module. You would use one to create web applications, and the other for bidirectional communication between a server and a client.
Here's a few of the things the modules can do:
Connect:
service static files and pages
provide cookie-based sessions
accept file uploads
handle HTTP verbs (GET/POST/PUT/DELETE)
Socket.IO:
authorize connecting sockets
send data between server and client with multiple transports
supports (WebSocket/XHR long-polling/flashsocket/JSONP)
So if you wanted to create a website, you would use Connect. However, if you wanted that website to have something such as realtime chat capability, then you would use Socket.IO.
Whether you should use one module or the other, or use them together, is dependent on your application requirements.
Connect is special module which can provide scalable functionality. You can just add features as middleware. It reminds some kind of configuration of your project, it just simplify routine.
var app = connect()
.use(connect.logger('dev'))
.use(connect.static('public'))
.use(connect.bodyParser())
.listen(3000);
After adding this for example you can access features which connect provide. For example you can have logging (method url and seconds) for each application activity, or add session support, easy with one line of code. The same way you can add socket support I suppose.

Resources