SignalR cannot establish a connection if it is on HTTPS - azure

So apparently I have 4 environment , 3 environments runs on HTTP and one in HTTPS. Which are all deployed on azure. I am using react-redux as my front end and Asp.net MVC5 as my backend. I tried deploying and testing the signalR to the 3 environments that is running on HTTP and it works like a charm , but when I deployed it on HTTPS. It doesn't work. I have properly configured everything. Since the SignalR is used by me as a chat System and the others where running like REST Apis (endpoints etc. ) My question here is , is the problem in the SSL Certificate of the signalR? I have tried various searches but no luck. Here is my Client Side code since I think the problem is here.
connectToSignalR(){
const con = $.connection;
let hubConnection = con.activeViewersHub;
const { prop1, match: { prop2}, actions: {getViewingUsers} } = this.props;
hubConnection.client.getMessage= (response) =>{
getMessages(response);
};
con.hub.url = `${URL}/signalr`;
con.hub.start({withCredentials:true,transport: 'longPolling' })
.done(()=>{
hubConnection.server.addMessage(prop1,prop2);
});
}
my point is that SSL is properly configured in azure. Since the system is running and everything is working fine, only in signalR that it doesn't work. Been spending a week already in this scenario. Any thoughts about this?

Related

connect by socket.io for react native and sails

I am trying to connect mobile app with react native and nodeJS running with sails
I have test the connection with reactJS and the socket work fine on the browser but with react native it give me this error
The socket was unable to connect.
The server may be offline, or the
socket may have failed authorization
based on its origin or other factors.
You may want to check the values of
`sails.config.sockets.onlyAllowOrigins`
or (more rarely) `sails.config.sockets.beforeConnect`
in your app.
More info: https://sailsjs.com/config/sockets
For help: https://sailsjs.com/support
I have tried to add onlyAllowOrigin and add on it the url for connect and I have added origin in the header for the frontend part but all didn't work
this is the setup I used in frontend
let socketIOClient = require('socket.io-client');
let sailsIOClient = require('sails.io.js');
let token = 'my jwt token'
let io = sailsIOClient(socketIOClient);
io.sails.url = 'http://localhost:1337/';
io.sails.headers = {authorization:token,origin:'http://localhost:3000'}
useEffect(() => {
io.socket.on('my event',(data) => {
console.log({data})
})
})
backend setup
var io = sails.io
io.emit('my event, {data:'my data'});
I think your issue may be that, as you said you are testing on a 'device' if that is the case then
io.sails.url = 'http://localhost:1337/';
Will not work, the device has no access to localhost, you need to make the socket server publicly available, even if you are using wifi. Localhost is just that, local to the host. Your app can access it on the desktop because, well its local but when its on the device it will try and connect to itself on port 1337, but nothing is running there so its failing.
I would suggest just pushing your code to a public server of some sort (socket server that is)
Some inexpensive / free location are
AWS (free tier)
Digital Ocean ( 100.00 credit or 5.00 / month)
Linode
(100.00 credit or 5.00 / month)
There are many others, but these are some popular ones, also with all options they have pre configured node instances that make it easy to get up and running quickly.

Nodejs ssl3_get_record wrong version number

For the past few months I've been working on a project involving Nodejs (12.16.1).
I've created a backend API (database and other resources) and an authentication service. Im currently updating a Discord bot that uses nodejs (all using the same node version and same node-fetch).
The bot is having issues connecting to the Backend API. Using node-fetch with the following error
request to https://<host:port>/api/bot/<resource> failed, reason: write EPROTO 1988:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:c:\\ws\\deps\\openssl\\openssl\\ssl\\record\\ssl3_record.c:332:\n
Im not using a Proxy, infact its a dev environment all running under localhost. Which has been running without an issue for months.
Now for the interesting part. The bot will POST fine to the authentication service but will fail when using POST or GET to the Backend. Postman will have no issue accessing the APIs. Thinking it was the certificate I made a new one (self-signed) but i still get the same error message.
So I used Wireshark and captured the traffic between the Bot the Authentication service and the Backend. When the bot talks to the Authentication services it does the Hello client / server messages in TLS 1.3. However when it talks to the backend it tries to use TLS 1.0.
Thinking any new current code was causing issues i pulled a fresh copy of the Backend and the Bot from my repo and tried again. Even the current Live builds will fail with the same error message.
I have no clue what is going, I've only been coding for a few months and this is the first wall I've hit that i can't seem to figure out. Goolge and even stack hasn't been much help as an ssl3 wrong version errors have been left unanswered or don't line up with my issue (most people seem to have this issue when using Proxies).
The only thing that has updated software wise was the latest Windows 10 updates.
KB4598242, KB4023057
Thanks.
Edit.
server.js
const port = process.env.PORT;
const key = fs.readFileSync('./server.key');
const cert = fs.readFileSync('./server.cert');
app.use(jwt());
app.use(express.json());
app.use(cors());
app.use('/api/bot', require('routes/botapi'));
const server = https.createServer({'key': key, 'cert': cert}, app);
server.listen(port, () => { console.log(chalk.green(`[SERVER]`) + `: listening on ${port}`) });
botapi.js
...requires
const botRouter = express.Router();
botRouter.post('/updatemembers', discordUpdateMembers);
async function discordUpdateMembers(req, res, next) {
let validRole = await hasBotRole(req);
if (!validRole) return res.status(400).send({err: 'Access_Denied!!'});
discord.updateMembers(req.body)
.then(members => members ? res.json(members) : res.status(400))
.catch(err => next(err));
}

How to redirect api requests to express port on WAMP

I have a React app making requests to an express api server which is running on port 9000.
In dev I use a proxy setting in my package.json which works as expected:
"proxy": "http://localhost:9000/"
I have now built and deployed both the api and the client and have deployed them both to the www folder on WAMP. The api is working fine as I can interact with it using Postman. The client is also up and running however it is now trying to call the api on the wrong port:
http://localhost/api/...
How can I redirect the api requests to port 9000 on WAMP?
http://localhost:9000/api/...
The proxy feature is not for the production. It is meant to be used for development only. Proxy in development is just a productivity feature. It’s useful if you serve the single-page app from your API server in production, but want to use the development server provided by create-react-app while you work on the app. However proxy is just that: a development feature. It is not meant for production.
You can read more about it here. If you really want to point a port, add it to app.js of your node application and consider starting server on that port.
In the end I decided to remove the proxy setting and prefix the requests with the correct api domain. This way it works as expected on both dev and prod:
private domain = "http://localhost:9000";
get(url: string, params?: any): Promise<any> {
return new Promise(async (resolve, reject) => {
try {
const result = await trackPromise(
axios.get(this.domain + url, { params: params })
);
return resolve(result.data);
} catch (error) {
return reject(error.response.data.message);
}
});
}

socket.io + azure app services 503 error even though websockets is ENABLED

I am trying to set up socket.io with my Node.js server on Azure app services. It works perfectly fine in my local server. However, I can't seem to get it to work on Azure.
I have enabled web sockets in my Azure App Services -> configuration -> general settings. However, this does not work.
I have followed instructions in this stackoverflow post: Socket IO net::ERR_CONNECTION_REFUSED but it hasn't worked for me.
My server side code:
...
const port = process.env.PORT || 5000;
const app: Application = express();
const server = require("http").Server(app);
const io = require("socket.io")(server);
...
io.of("/chat").on("connection", async function(socket: any) {
console.log('hi')
});
...
server.listen(port, () => console.log(`listening on port ${port} `));
My CORS settings have also been set up appropriately, HTTP requests to my server work fine, as well as requests that require a credentials.
Any help would be appreciated. Thanks
EDIT (MORE INFO):
I am using in-house authentication. Because of this, I need to set the header 'Access-Control-Allow-Credentials' to TRUE. My express app was setting this to true via the cors npm module, however no matter what I did I could not get HTTP requests to work when doing authentication requests.
I was able to solve this by doing:
az resource update --name web --resource-group <myResourceGroupName> --namespace Microsoft.Web --resource-type config --parent sites/<site-name> --set properties.cors.supportCredentials=“true” --api-version 2015-06-01
which i found here: (https://learn.microsoft.com/bs-latn-ba/azure/app-service/app-service-web-tutorial-rest-api)
This allowed HTTP requests with credentials to work, as well as any other HTTP requests. It is important to note that in my Azure settings, all my CORS settings are still blank in order to allow my express app to handle CORS.
Not sure if this is related...
EDIT2:
when I try to connect from my client with
const socketIo = io(socketUrl + "/chat", {
// transports: ["websocket"]
upgrade: false,
})
everything works fine. But if i uncomment transports: ["websocket"], connection will fail. It's got to have something to do with the websocket settings in Azure
I would suggest you to provide a minimal working example (server and a client page) it would simplify things.
Here are a couple things to check:
Disable perMessageDeflate in your server-side Node.js code
for example like this:
const io = require('socket.io')(server,{
perMessageDeflate :false
});
Azure Web Apps only listens on ports 80 & 443 which is a common issue
It doesn't look like a case here, but it happens so often, so I will leave it here to help others. According to the doc:
Q: Can I expose more than one port on my custom container image?
A: We don't support exposing more than one port.
So, if this is a case - change the port to either of them and your app will work fine.
I hope it helps! 🙂
So I made a barebones example to test the socket connection... and it worked.
Then, I tried my app again.. and it worked. Dunno what happened but everything is working now...

How to cache last loaded data on the client in Cordova app?

I'm building an app on Meteor and have been testing my app on my Android device. It works great. I'm publishing my data on the server and subscribing to it in the client.
But when I don't have data or the server is down, the mobile app shows no content. This makes sense as it can't load anything. But it must be possible to cache the last loaded data from the server so there is at least something to display in the mobile app when there is no connection.
I looked into appcache but that seems to be for assets as opposed to database content. Does anybody know how to make the Cordova version of a Meteor app cache the database locally so it will also work offline?
To cache our iPad application configuration we use localStorage. It's super easy to use and really worked out well for use.
localStorage.settings = settings; // data from server
and when the application starts:
var settings = null;
if( navigator.network.connection.type === Connection.NONE ) {
settings = localStorage.settings
} else {
// fetch settings from server
}
We've been running this implementation for over a year now without any issues.

Resources