Single Webscoket for 2 clients - websocket

I'm trying to build a chat app and i want to use web socket for chat data betwenn two clients.
How can I implement a web socket such that only 2 clients connected to a socket and I want many users to use the socket simultaniously without them connecting to other sockets so that there wont be unauthorised chat.
example
user 1 and 2 connect to socket at 192.168.0.1/uid1
user 3 and 4 connect to socket at 192.168.0.1/uid2
is something like that possible.
Thanks in Advance.

Related

How to store websocket id in Gorilla using Redis for accessing the socket connection across the processes

I would like to know the best way to store the websocket client id or connection in Redis. I am not building a chat app so I do not need to broadcast but send messages to specific websockets.
Need help.

2 Web Socket Connection vs 1 Web Socket Connection

I had a Chat application which communicated with Chat server over single web socket connection.
Now I am planning to introduce a middle man (web service) between Client page & chat server.
why middle man service is another can of worm.. but lets assume its absolutely required.
so my question is where having this 2 web sockets connection Is it a good design ? Can i tie 2 web socket connection so that if client sends to middle man i can transform that message & 2nd web socket connection picks it up ? visa versa ?

Socket.io vs RTCDataChannel, as Signalling Servers?

I am new to this !
I am working for a chat application which requires text+ video chats.
I explored Socket.io initially and found it very handy to develop text based chatting application (WEB).
While exploring the Video chat element i came across WebRTC -RTCDataChannel for sending out arbitrary data across connected peers.
My Chat Server( preferably NodeJS ) will be serving the connections for peers, along with saving text chat history.
Confusion:
Should I use Socket.io-MyChatServer as the Signalling server also? [Possible?] , Or
Should I use RTCDataChannel for signalling server? , Or
Simply forget Socket.io and consider WebRTC for both !
Thanks in advance :)
Well WebRTC data channels and web sockets are different and complementary concepts in the case of peer connections.
In order to open a data channel you first need a P2P connection. In order to establish a P2P connection, you need a signaling server. So, sockets are used for that purpose, to exchange the metadata necessary to create a P2P connection. First, through sockets you establish a peer to peer connection and only after that you can use data channels.
As for using the same chat server as signaling server is up to you. WebRTC let the signaling server architecture be defined by the developer. It's a blackbox.
So, no you can't use data channels as signaling, as you can see.

How does SailsJS handle push messages to connected sockets with more dyno's in Heroku?

I've got a question about scaling a SailJS app on Heroku.
The setup :
SailJS
Kue (Redis queueing system)
Connected clients by means of socket io (sailsjs)
2 dyno's (each run a SailsJS instance)
CronJob kind of process
I want to autonomously run a continuous 'cron' process, which triggers a push message, which must be sent to a connected socketio client.
I am aware the socket connections reside in Redis, when this is configured correctly.
I use kue for nodejs, and trigger a worker to process the task.
The worker is situated in the SailsJS instance, to be able to send a connected socket client a push message.
But I wonder, not every SailsJS instance (dyno) has the same connected sockets right? In other words, does every SailsJS instance has the same connected socket clients available, or is it possible dyno1 has a set of connected clients, and the other dyno the other set of clients??
e.g:
Heroku Cloud => Cron => Kue (Redis)
=> Dyno 1 (SailsJS instance) => client 1
=> client 2
=> Dyno 2 (SailsJS instance) => client 3
=> client 4
How does one solve this... :-D?
To give a lead to a potential answer:
Seems the guys at sails.js are working on this now! See: https://github.com/balderdashy/sails/issues/2593
I hope these guys wil enable the 'subscribers()' method in behaving in same fashion as e.g. 'message()' does.
In my opinion when the method: 'message()' enables you the send a message to client connected to another sails instance, the method 'subscribers()' should return subscribers (socket id's) connected to both sails instances or it should be really nice to have to know if a instance is able to know if a specific client is connected.
Thanks!
What I do is to subscribe a socket to its own room (usually the room is the user Id). This way, you don't need to track the socket id (which changes for every browser connection). When any part of my app needs to "talk" to a connection, it emits a message to the room. This is good because it allows a user with multiple devices to receive the same message (just like when you have Skype on your desktop and on your phone, and you can chat using any of them interchangeably).
So, user A connects with the desktop, I subscribe his/her socket 1235 to his/her room UserA. If user A connects at the same time with a phone, I subscribe socket 4567 to room UserA. If any worker needs to send a message, it emits to room UserA. You can use socket.io-emitter: https://github.com/socketio/socket.io-emitter
var io = require('socket.io-emitter')({ "host" : REDIS_WEBSOCKET_HOST, "port" : REDIS_WEBSOCKET_PORT });
io.in("userA").emit("message", "Some message");
EDIT
Also, remember Sails has a broadcast feature:
http://sailsjs.org/documentation/reference/web-sockets/sails-sockets/sails-sockets-broadcast
You can send a message to a room. If your message is global, you can have all your sockets subscribe to a global room and any Sails instance will be able to communicate with everyone. If you still want to track individual sockets, you can still use my technique above and use sails broadcast to that room instead of socket.io-emitter.

zeromq DEALER client to multiple servers (ROUTER)

I am using ZEROMQ for distributed messaging application. Need to connect client (DEALER socket) to multiple servers (ROUTER socket on server side). What are my options on CLIENT side ?
Create DEALER socket on client side for each server endpoint (ROUTER socket).
Create only ONE DEALER socket on client side and add multiple endpoints.
I tried option 2 - connecting to multiple endpoints but message always goes to the first connected endpoint. followed following steps:
create DEALER socket
connect to first endpoint
then at run time, add another endpoint to the socket by using socket.connect(endpoint).
Do I need to reconnect?
In DEALER socket, there is no option to send message on a particular endpoint in case it is connected to multiple endpoints.
Any idea?
ZeroMQ encodes certain behaviors into socket types. These mainly deal with:
handling multiple peers
handling undeliverable messages
handling excessive throughput (HWM)
A DEALER socket is one that can connect to multiple peers, and uses LRU (least recently used, aka round-robin) to decide which peer gets each message. If you do not want this behavior, then you do not want a DEALER socket with multiple peers.
If you want to decide which peer gets a message, there are two options for this:
create a DEALER per peer, and send on the appropriate socket
create a single ROUTER connected to all peers, and use IDENTITY prefixes to route messages. You may need to pass IDENTITIES via a side channel, in order to use ROUTER-ROUTER connections.
at run time, add another endpoint to the socket by using socket.connect(endpoint). Do I need to reconnect?
No, you do not need to reconnect. You can add (and remove) peers at any time during the program.

Resources