简体   繁体   中英

Need to know something regarding socket.io and redis and nginx

My goal is to build a chat application - similar to whatsapp

To my understanding, socket.io is a real-time communication library written in javascript and it is very simple to use

For example

 // Serverside
    io.on('connection', function(socket) {
        socket.on('chat', function(msg) {
            io.emit('chat', msg);
      });
    });

 // ClientSide (Using jquery)

   var socket = io();
  $('form').submit(function(){
    socket.emit('chat', $('#m').val());
    $('#m').val('');
    return false;
  });

  socket.on('chat', function(msg){
    $('#messages').append($('<li>').text(msg));
  });

1) do I always need to start an io.on('connection') to use the real-time feature or i could just start using socket.on object instead? for example i have a route

app.post('/postSomething', function(req, res) {
    // Do i need to start an io.on or socket.on here?

});

because i want the real-time feature to be listen only on specific route.

2) Redis is a data structure library which handles the pub/sub, why do we need to use pub/sub mechanism? I read alot of articles but couldn't grasp the concept. Article example http://ejosh.co/de/2015/01/node-js-socket-io-and-redis-intermediate-tutorial-server-side/

for example the code below

// Do i need redis for this, if so why? is it for caching purposes?
// Where does redis fit in this code?


 var redis = require("redis");
 var client = redis.createClient();


 io.on('connection', function(socket) {
     socket.on('chat', function(msg) {
          io.emit('chat', msg);
      }); 
  });

3) Just wondering why I need nginx to scale node.js application? i found this stackoverflow answer: Strategy to implement a scalable chat server

It says something about load balancing, read that online and couldn't grasp the concept as well.

So far I have only been dealing with node.js , mongoose simple CRUD application, but I'm willing work really hard if you guys could share some of your knowledge and share some useful resources so that I could deepen my knowledge about all of these technologies.

Cheers!

Q. Socket.on without IO.on

io.on("connection" ... )

Is called when you receive a new connection. Socket.on listens to all the emits at the client side. If you want your client to act as a server for some reason then (in short) yes io.on is required

Q. Redis pub/sub vs Socket.IO

Take a look at this SO question/anwer , quoting;

Redis pub/sub is great in case all clients have direct access to redis. If you have multiple node servers, one can push a message to the others.

But if you also have clients in the browser, you need something else to push data from a server to a client, and in this case, socket.io is great.

Now, if you use socket.io with the Redis store, socket.io will use Redis pub/sub under the hood to propagate messages between servers, and servers will propagate messages to clients.

So using socket.io rooms with socket.io configured with the Redis store is probably the simplest for you.

Redis can act like a message queue if it is a requirement. Redis is a datastore support many datatypes.

Q. Why Nginx with Node.js

Node.js can work standalone but nginx is faster to server static content.

Since nginx is a reverse proxy therefore servers are configured with nginx to handle all the static data (serving static files, doing redirects, handling SSL certificates and serving error pages. ) and every other request is sent to node.js

Check this Quora post as well: Should I host a node.js project without nginx? Quoting:

Nginx can be used to remove some load from the Node.js processes, for example, serving static files, doing redirects, handling SSL certificates and serving error pages.

You can do everything without Nginx but it means You have to code it yourself, so why not use a fast and proven solution for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM