Client-Server architecture behind chat applications using socket.io - node.js

I am learning socket programming using socket.io module. I couldn't understand the client-server architecture behind the application programmed using socket.
For example, A is chatting with B using a chat application, both A and B are hosts similar to peer-to-peer architecture.
Where does the server come into this picture?

A will be connected P2P to the server.
B will be connected P2P to the server.
So when A send a message, the server print it out to all other client (B in this context). Exacly the same for B.
A and B arn't connected each other. But you could do it.

there is no peer-to-peer connections with socket.io.
all the communications are managed server-side and you can have different namespaces and rooms. for more info look here.
there are different transport protocol that you can use, such as HTTP or WebSockets.
if you want to scale up the machines using a load balancer be aware that you have to implement a sticky session load balancing. for example you can use the ip_hash option of nginx.

Related

NodeJS how to secure socket.io sessions across different countries

I'm making a nodejs application that will act a server for other sites in different countries as the data being transmitted will be business related data. I would like to know how I can safely/securely send this data.
I am currently using socket.io to act as my main server (Master) on other sites there are (Slave) servers that handle the data from the master server.
I have got this working in a local environment but want to deploy this in the other sites.
I have tried to Google this to see if anyone else has done this but came across socket.io sessions but I don't know if this will fit with (Server->Server) connections.
Any help or experience would be grateful.
For server-server communication where you control both ends of the communication you can use WebSocket over HTTPS, you can use TCP over SSH tunnel or any other encrypted tunnel. You can use a PubSub service, a queue service etc. There are a lot of ways you can do it. Just make sure that the communication is encrypted either natively by the protocols you use or with VPN or tunnels that connect your servers in remote locations.
Socket.io is usually used as a replacement for WebSocket where there is no native support in the browser. It is rarely used for server to server communication. See this answer for more details:
Differences between socket.io and websockets
If you want a higher level framework with focus on real-time data then see ActionHero:
https://www.actionherojs.com/
For other options of sending real-time data between servers you can use some shared resource like a Redis database or some pub/sub service like Faye or Kafka, or a queue service like ZeroMQ or RabbitMQ. This is what is usually done to make things like that work across multiple instances of the server or multiple locations. You could also use a CouchDB changes feed, or a similar feature of RethinkDB to make sure that all of your instances get all the data as soon as it is posted by any one of them. See:
http://docs.couchdb.org/en/2.0.0/api/database/changes.html
https://rethinkdb.com/docs/changefeeds/javascript/
https://redis.io/topics/pubsub
https://faye.jcoglan.com/
https://kafka.apache.org/
Everything that uses HTTP is easy to encrypt with HTTPS. Everything else can be encrypted with a tunnel or VPN.
Good tools that can add encryption for protocols that are not encrypted themselves (like e.g. the Redis protocol) are:
http://www.tarsnap.com/spiped.html
https://www.stunnel.org/index.html
https://openvpn.net/
https://forwardhq.com/help/ssh-tunneling-how-to
See also:
https://en.wikipedia.org/wiki/Tunneling_protocol
Note that some hosting services may give you preconfigured tunnels or internal network interfaces that pass data encrypted between your servers located in different data centers of that provider. Some providers give you tools and tutorials to that easily as well.

Horizontally scale socket.io with redis

I currently am creating a horizontally scalable socket.io server which looks like the following:
LoadBalancer (nginx)
Proxy1 Proxy2 Proxy3 Proxy{N}
BackEnd1 BackEnd2 BackEnd3 BackEnd4 BackEnd{N}
My question is, with socket-io redis module, can I send a message to a specific socket connected to one of the proxy servers from one of the backend servers if they are all connected to the same redis server? If so, how do I do that?
As you wan to scale socket.io server, and you have used nginx as load balancer, do not forget to setup sticky load balancing, othersie single connection will be connected to multiple server based on load balancer pass the connection to socket.io server. So better to use sticky load balancing
With the redis socket io adapter, you can send and receive message with one or more socket.io server with help of Redis Pub/Sub implementation.
if you tell me which technology is used for Proxy and Backend, i will let you know more information on this.
Using the socket.io-redis module all of your backend servers will share the same pool of connected users. You can emit from Backend1 and if a client is connected to Backend4 he will get the message.
The key for this working though with Socket.io is to use sticky sessions on nginx so that once I client connects, it stays on the same machine. This is because the way that socket.io starts with a WebSocket and several long polling threads, they all need to be on the same backend server to work correctly.
Instead of sticky sessions, you can change your client connection optons to use Websockets ONLY and this will remove the problems with the multiple connections to multiple servers as there will only be one connection, the single websocket. This will also make your app lose the ability to downgrade to long-polling instead of WebSockets.

Full-duplex messaging between remote autonomous Node.js applications over WebSockets?

There will be no human being in the loop, and both endpoints are autonomous Node.js applications operating as independent services.
Endpoint A is responsible for contacting Endpoint B via secure web socket, and maintaining that connection 24/7/365.
Both endpoints will initiate messages independently (without human intervention), and both endpoints will have an API (RESTful or otherwise) to receive and process messages. You might say that each endpoint is both a client of, and a server to, the other endpoint.
I am considering frameworks like Sails.js and LoopBack (implemented on both endpoints), as well as simply passing JSON messages over ws, but remain unclear what the most idiomatic approach would be.
Web Sockets have a lot of overhead for connecting to browsers and what not, since they try to remain compatible with HTTP. If you're just connecting a pair of servers, a simple TCP connection will suffice. You can use the net module for this.
Now, once you have that connection, how do you initiate communication? You could go through the trouble of making your own protocol, but I don't recommend it. I found that a simple RPC was easiest. You can use the rpc-stream package over any duplex stream (including your TCP socket).
For my own application, I actually installed socket.io-client and let my servers use it for RPC. Although if I were to do it again, I would use rpc-stream to skip all the overhead required for setting up a Web Socket connection.

How to Install XMPP on Linux 32-bit OS

I am trying to install xmpp on Linux, i found the following links
build a chat server using openfire on centos
set up IM on CentOS
IM on Cent OS
I have an IOS application and i need to build an IM using jabber/xmpp in it. There by allowing users on the application to chat among themselves.
To do this i am trying to install XMPP on the server. So, I want to know how the communication is established between
Application User-- Server -- Application User
How a User utilizes the xmpp configuration to send messages to another user ?
Can't IOS itself have the configuration? Is it necessary to install XMPP on server to accomplish this task?
The examples i came across so far based on Window--window application(Web based).
Can anybody explain the actual process of communication take place? How an IOS
user connects to XMPP installed on my server?
Yes you can install XMPP with any of of your favourite links you mentioned to understand how XMPP works A small overview is below and for more details you can see http://xmpp.org/rfcs/rfc3920.html & http://ceit.uq.edu.au/content/how-xmpp-works-step-step
XMPP is not wedded to any specific network architecture, to date it usually has been implemented via a client-server architecture wherein a client utilizing XMPP accesses a server over a [TCP] connection, and servers also communicate with each other over TCP connections.
The following diagram provides a high-level overview of this architecture (where "-" represents communications that use XMPP and "=" represents communications that use any other protocol).
C1----S1---S2---C3
|
C2----+--G1===FN1===FC1
The symbols are as follows:
C1, C2, C3 = XMPP clients
S1, S2 = XMPP servers
G1 = A gateway that translates between XMPP and the protocol(s) used on a foreign (non-XMPP) messaging network
FN1 = A foreign messaging network
FC1 = A client on a foreign messaging network
Server
A server acts as an intelligent abstraction layer for XMPP communications. Its primary responsibilities are to manage connections from or sessions for other entities, in the form of XML streams to and from authorized clients, servers, and other entities
to route appropriately-addressed XML stanzas among such entities over XML streams
Most XMPP-compliant servers also assume responsibility for the storage of data that is used by clients (e.g., contact lists for users of XMPP-based instant messaging and presence applications); in this case, the XML data is processed directly by the server itself on behalf of the client and is not routed to another entity.
Thanks & Regards,
Alok

Language related -What does Client-server application mean?

Well
It's not a big question, obviously.
But you see, an application that is using a database on the servers, and is installed on multiple clients. Is called Client/Server application.
And an application that is constituted by two parts: Host (or server) part, and the client part.
They are both called client/server apps
How can we distinguish them, and what's the proper name of each type.
P.S. I'm talking about English, you know. I want to say that this application is Client/Server(only database on server) and not a Client/Server(as real client and server).
As others have said, they're both client/server applications. If you want to be more explicit about what kind of client and what kind of server, you can use adjectives or nouns. Please don't go inventing new terms when we've already got plenty of terms that can be combined - computer science & software engineering are already overloaded with unnecessary terms.
web client/web server (note that the web includes more protocols than just HTTP)
http client/http server
ftp client/ftp server
soap client/soap server
client/application server (the app server may include a db, or the appserver may be the db client)
database client/database server
JDBC client/database server
sqlplus/oracle
application server/database server
I don't think I could say it any better than Wikipedia:
Client-server computing or networking
is a distributed application
architecture that partitions tasks or
work loads between service providers
(servers) and service requesters,
called clients. Often clients and
servers operate over a computer
network on separate hardware. A server
machine is a high-performance host
that is running one or more server
programs which share its resources
with clients. A client does not share
any of its resources, but requests a
server's content or service function.
Clients therefore initiate
communication sessions with servers
which await (listen to) incoming
requests.
I am not sure what you are asking in the second part of your question - if both architectures involve clients making requests to servers, then they are both client-server architectures. On what basis would you prefer to distinguish them?
It has to do with which side the request comes from (amongst other things): the Client issues a "request" and the Server responds.
The terminology Client-Server is often associated with a discipline called "System Design Engineering" (there are other attributions for sure but that's one).
Of course, an Agent could be composed of different sub-systems, each having different roles (i.e. S1 has a Client role whilst S2 has a Server role).
One should note that the terminology Client-Server can also be found outside of Computer Sciences. A good example is in the networking field: A Client protocol layer interfaces with a Server protocol layer e.g. IP (Client) uses the services of an Ethernet protocol layer (the Server in this case).

Resources