Socket.io - server down handling - javascript

I am trying to write simple app that serves data using socket.io and node.js on server side. I would like to inform user if server is down (e.g. server serves webpage to the client and before any refresh server is halted). For now it results in browser giving following error:
GET http://localhost:8080/socket.io/?EIO=2&transport=polling&t=1409325689935-6 net::ERR_CONNECTION_REFUSED
I wasn't able to catch that event using socket.on('error'(...)) event and I would like to display some message as long as client fails to connect to server. Any ideas would be appreciated :) Code initializing socket is provided below:
socket = io.connect(document.domain, {
'sync disconnect on unload' : false
});

Couldn't you just check if the server serves you the .js file by doing something like this?
<script type="text/javascript" onload="loaded()" src="get_socket.js"></script>
function loaded(){
//this will only execute if the server is up
}

I have successfully used this following code for not only socket.io but any .js you wish to load, execute, or both. Please forgive me as I have long forgotten where I originally got this from, most likely this very website.
Please note that I have only tailored the loadAndExecute function to handle inability to load your file, via callback.
var load,execute,loadAndExecute;
load=function(a,b,c){var d;d=document.createElement("script"),d.setAttribute("src",a),b!=null&&d.addEventListener("load",b),c!=null&&d.addEventListener("error",c),document.body.appendChild(d);return d},
execute=function(a){var b,c;typeof a=="function"?b="("+a+")();":b=a,c=document.createElement("script"),c.textContent=b,document.body.appendChild(c);return c},
loadAndExecute=function(a,b,c){return load(a,function(){return execute(b)},function(){return c();})};
Use as such:
loadAndExecute("domain.com:80/socket.io/socket.io.js", function() {
//it loaded, put main code here
},
function(){
//failed to load, put error display code here
});
Hopefully this can help someone like it helped me so many times.

Related

How to hide js socket connection?

I've recently build an socket with Ratchet (http://socketo.me/) in Symfony ( PHP framework ) with JS/jQuery. The problem im facing is that the socket IP+PORT are just visible in the js file, so everyone can connect with it using the console or something.
I'm executing several things with the socket, that impacts everyone's view that's connected with it.
Now I'm just able to open a new connection through the console using:
var socket = new socket('http://www.IP:PORT');
Then I'm able to use the send command to execute things that only the server should have access to. Like this:
socket.send(JSON.stringify({info: 'information', action: 'runAction'}));
Is there anyone that can explain me how to keep these send calls privately so not everyone can just have access to these actions and call them?
You try to have these things stored in your php code and make an api say /getSocketDeatils
Now make an authenticated REST call to your api and make the socket when your app starts.
This is just one way there can be multiple other

Using Raven.js is there a way to determine when an error is successfully logged to the Sentry server?

I'm building an Ionic app and using Sentry for logging. I'm calling Raven.captureMessage() and I need to know when the error is successfully logged so I can remove it from the local cache.
I'm saving the errors in a local cache so I can resend them at a later time if there is a connection issue.
There's an undocumented "events" API in Raven.js that works like so:
document.addEventListener('ravenSuccess', function (evt) {
console.log(evt.data); // event data
});
I'd recommend looking at the source code to see exactly how this works: https://github.com/getsentry/raven-js/blob/master/src/raven.js#L1011
We've also started a discussion on GitHub on re-doing the events API – your feedback would be greatly appreciated: https://github.com/getsentry/raven-js/issues/524

Node JS for Google Mirror API

I am trying to implement the Oauth using Node JS for Glass Mirror API amd found this useful. In the "app.js" file, we need to provide the credentials of the project created in the Google developer console.
I have the client ID, client secret but am not able to define what should my callback URL be? What should be in the callback URL script? Logically I understand that there should be a program that accepts the token and runs the further steps.
But how to write one? kindly help.
Thanks in advance
base on the sample code from your link, the callback URL will be
// running in localhost
http://localhost/oauth2callback
// or running on your server
http://yourdomain.com/oauth2callback
when the google redirect to oauth2callback, your server code will run to redirect back to index page
grabToken(req.query.code, failure, function () {
res.redirect('/');
});

PHP minimal working example of Web Sockets

I'm trying to determine how to setup a web socket for the first time ever so a working minimal example with static variables (IP address for example instead of getservbyname) will help me understand what is flowing where.
I want to do this the right way so no frameworks or addons for both the client and the server. I want to use PHP's native web sockets as described here though without over-complicating things with in-depth classes...
http://www.php.net/manual/en/intro.sockets.php
I've already put together some basic JavaScript...
window.onload = function(e)
{
if ('WebSocket' in window)
{
var socket = new WebSocket('ws://'+path.split('http://')[1]+'mail/');
socket.onopen = function () {alert('Web Socket: connected.');}
socket.onmessage = function (event) {alert('Web Socket: '+event.data);}
}
}
It's the PHP part that I'm not really sure about. Presuming we have a blank PHP file...
If necessary how do I determine if my server's PHP install has this socket functionality already available?
Is the request essentially handled as a GET or POST request in
example?
Do I need to worry about the port numbers? e.g. if
($_SERVER['SERVER_PORT']=='8080')
How do I return a basic message on the initial connection?
How do I return a basic message say, five seconds later?
It's not that simple to create a simple example, I'm afraid.
First of all you need to check in php configuration if the server is configured for sockets with the setting enable-sockets
Then you need to implement (or find) a websocket server that at least follows the Hybi10 specification (https://datatracker.ietf.org/doc/html/draft-ietf-hybi-thewebsocketprotocol-10) of websockets. If you find the "magic number" 258EAFA5-E914-47DA-95CA-C5AB0DC85B11 in the code for the header, you can be sure it does follow at least Hybi06 ...
Finally, you need to have access to an admin console on the server in order to execute the PHP websocket server using php -q server.php
EDIT: This is the one I've been using a year ago ... it might still work as expected with current browsers supporting Websockets: http://code.google.com/p/phpwebsocket/source/browse/trunk/+phpwebsocket/?r=5

Disable meteor registration

I want to disable registration from my Meteor app. I'm using the accounts-ui smartpackage.
I tried this:
Accounts.config({
var forbidClientAccountCreation = true;
})
but my app server crashes. How can I fix this?
This is using one universal JS file, not one for client and one for server.
Accounts.config takes one parameter which is a javascript hashmap. You should write it correctly:
Accounts.config({
forbidClientAccountCreation : true
});
I've just ran into this and the answers here aren't completely clear. Accepted answer works for the OP because he's using a single JS file, but if not, place the following code in a file outside the client and server folders.
Accounts.config({
forbidClientAccountCreation : true
});
The reason is that running it on the client will trigger the accounts-uifeature of hiding the "Sign up" links and text, and running it on the server will actually forbid new user accounts from being created.
If you only run it on the client, the links and text will be hidden but you can still create an account through the browser's console.
If you only run it on the server, account creation will always fail but you'll still get the associated links and text.
A good place for the code is in the lib folder, because anything in that folder will be processed by Meteor both on the server and the client, and also before any other folder. For example, you could place it in lib/environment.js.
Just type next on console:
Accounts._loginButtonsSession.set('inSignupFlow', true);
and you will see the registration form again.
What I did was to prevent validation of the user creation on server as follows:
Accounts.validateNewUser(function (user) {
return false
});

Categories