Gets multiple events call back from angular end socket.io - angularjs

(X) First Scenario
We have a web application and we are using socket.io as middleware. There is an angular end and there are API from .net. and it is a chat application, when a user sends a message it calls for the socket server and from socket server, we call to the API and we send the message to the particular socket. but when we send the message to the client the client event gets called multiple times(not every time but sometimes) from the angular end but the message, we have only sent once from the server(we set a logger to confirm this from server end). We have Azure Linux server. We are pooling socket ids from user ids as rooms. Though we removed rooms and added a manual array to pool sockets ids from user ids issue still exists. Working fine on android and ios devices.
****Second scenario
when we disconnect internet connection and reconnect the connection gets to connect to the server and we send a message after that it does send the message to other ends, but if we are sending messages from other end, messages are not coming to this end, from loggers in a server it shows that messages are sent. to the particular socket ids.
Setup
socket.io version: V2.0.3
node 6.11.4

Related

How do I refresh the token in a cometd client talking to a Salesforce Platform EventBus?

I'm using cometd to listen to platform events generated in Salesforce. My cometd client configuration code looks like
this.client.configure({
url: `${this.org.instance_url}/cometd/46.0`,
requestHeaders: {
Authorization: `Bearer ${salesforceToken}`
},
appendMessageTypeToURL: false
});
where the salesforceToken is obtained using the refresh token. This all works fine for a while but if there are no events for a considerable period of time (anecdotally anywhere between 6-24 hours), then my client seems to expire and no events are received. If I refresh the token and restart my listener, things start working again.
Is there a way to keep the listener active other than writing some sort of timer to restart the process every few hours after inactivity ?
You do not have to refresh the token again
Whenever there is no activity on the channel, the server closes the connection after a specific time.
In that time client receives a 403 (Unknown client) status code, and the client has to handshake again withing 110 seconds.
By default, CometD tries to reconnect without any user interaction and If the client doesn't reconnect within the expected time, the server removes the client's CometD session.
Once the connection gets reconnect, all the channel subscriptions will be removed by ComedD, and we have to subscribe to the channel again in order to receive events.
In order to do so, we have to make use of meta/Handshake callback to resubscribe to the channel again.
see https://developer.salesforce.com/docs/atlas.en-us.api_streaming.meta/api_streaming/using_streaming_api_client_connection.htm, paragraph "After Invalid Authentication"
"Streaming API regularly validates the OAuth token or session ID while the client is connected. If client authentication is not valid, the client is notified with the 401::Authentication invalid error and an advice field containing reconnect=none. After receiving the error notification in the channel listener, the client must reauthenticate and reconnect to receive new events."

Camel PAHO routes not receiving offline messages while connecting back

I'm using apache camel xml based paho routes for the subscription, publication process. While online, everything works fine. But I'm not able to receive the offline message.
I have set the following.,
Constant Client ID
Clean Session is FALSE,
Both subscribed & published with QoS 2
With the standalone Program, it's getting all the offline messages. With the camel route it's not happening.
Finally, I was able to solve this one manually.
Camel PAHO Client is not populating the callback function before performing the broker connection. They are doing it only when the connection is made.
So, once the connection is success then the broker just sends all the offline messages. In this case, our client does not have callback handlers to handle these messages. So they are lost.
Other clients (IoThub Client) which uses the PAHO internally is doing it right by setting the callback and initiating the connection.

Does accept() at server side unblock for each HTTPS request from client?

I have a problem with making HTTPS requests from the client to the server.
My server is using Winsock with OpenSSL on top of the sockets, while my client is using WinHttp library. All these are done in C.
Below is a description of what happened (or how I understood it to be) at the server and client during a network communication:
At the server side, the application is blocked at accept() while waiting for connection to be made from the client. On the first HTTP request made by client using WinHttpSendRequest(), the accept() unblocks and creates a new thread. Then, accept() went into blocking state again until a new client connects to it.
In the thread at the server side, recv() receives the packet sent over by the client, then it sends over a response packet via send().
Upon receiving the response packet from server, WinHttpSendRequest() at the client side unblocks, before moving on to execute WinHttpReceiveResponse() and the rest of the code to read in the content of the response packet.
Meanwhile, recv() at the server side blocks until client sends over another request via WinHttpSendRequest(), and this goes on to and fro until the server sends a command to terminate the client.
This mechanism of using Winsock at the server side and WinHttp at the client side works for sending multiple HTTP requests from client to server over the same connection.
However, when I decided to add in a layer of encryption for this network communication, ie. client sends HTTPS instead of HTTP requests over, and server uses openSSL functions to receive and send packets (SSL_read() in replacement of recv() and SSL_write in replacement of send()), each time the client makes a WinHttpSendRequest(), accept() at the server side will unblock and return a new socket. In the previous scenario where encryption was not done and requests were HTTP, accept() at the server side will only unblock when it is the first request made by a new client; subsequent requests by the same client will be received through the blocking and unblocking of recv().
Just some things to note: For my client side, I have kept the handles for WinHttpOpen and WinHttpConnect open, while those of WinHttpOpenRequest and WinHttpSendRequest will be closed after each request and recreated for each new request. Also, packets received for both sides can be read properly after encryption (I know encryption was done as packets sniffed by Wireshark is unreadable).
I have been searching all over the internet to see if anyone has experienced the same thing, but to no avail :( Is there anyone who can explain to me why this is happening?

payment gateway using socket.io and nodejs

I am developing a payment gateway (with 2 factor authorization, 2FA) with node/express for server side and angular js for client side.
The communication from client --> server can be done via a POST request but I want the communication from server --> client to happen via socket.io.
Now for new user who initiates a payment, a POST request will be sent from the client to server, data will be processed and a response will be sent back. For a very very specific reason, the response cannot be sent as part of the res in the received POST request.
app.post('/receive', function(req,res){
data = req.body
res.send('success')
// do something with the data
// Details regarding 2FA will be sent later via socket.io
}
After some processing, data will be sent to the client regarding 2 factor authorization.
io.on('2fa', {name:name, oneTimePassword:qwerty //and additional details})
I can listen to the channel 2fa from the client side, but every message emited on this will go to all the clients. I need a way to send a message to specific clients only who initiate that particular payment process.
Each transaction will be encrypted using ECDH. A UUID will be available for the transaction, initiating customer as well as the order number for which the payment is being done.
Appreciate any guidance for the above.
Socket.io has rooms. If you want, let's say, only the buyer and the seller to hear about the authentication result, put them into a room and emit your messages to the room itself. The client side won't change. You just socket.emit from the client to the server and the server will put the client into a room with all other appropriate parties and all the subsequent communication will only be emitted to the room instead of the channel 2fa.
This is from an html5 multiplayer game I developed, when the client joins a game room, this is what happens on the server side:
socket.on('joingame', function(data){
if(livingplayers[data.room] && livingplayers[data.room][data.id]){
socket.emit('joinfail', 'That name is taken');
} else {
socket.join(data.room);
socket.emit('joingame', data);
}
});
So, as you can see here, I join the client into the game, and I responded from the same channel and I informed the client that he successfully joined the game and on the server side I put the client into the room he joined. Any subsequent in-game actions will be broadcasted only to the room that the client just joined here.

GAE Channel API message broadcast to only one client?

I'hv been trying to learn the Google App Engine's Channel API lately.
I tried to make a simple chat app, but I am reaching problems.
This is generally what i have done.
server opens a static channel:
token = channel.create_channel('bigboys')
client js connect to channel with:
var channel = new goog.appengine.Channel(token)
I am not using Google App Engines users. I don't want clients to have to login.
So my problem is, the chat app doesn't really work, only one client can join the channel. I know there is a one client ID per channel rule. So how do I support multiple clients in one "chat room"?
Think of a channel being the connection between one client (browser) and your server.
If you have a chatroom, you'll need a channel for each client. You'll need something on the server side to keep track of all the clients in the chatroom, and the channel for each client. When you send a message, you'll need to send it on every client channel in the chatroom.

Resources