Socket.io emit event not firing from client side - ios

It may be something obvious but I don't understand why I don't receive my event in the server side
Server :
io.sockets.on('connection', function (socket, pseudo) {
socket.on('clickOnGraph', function(){
console.log('Reception of the first sending');
socket.broadcast.emit('clickOnGraph')
console.log('Broadcasting to everyone');
});
Client :
$scope.clickOnGraph = function(){
console.log('click detected, first sending to server');
socket.emit('clickOnGraph');
}
socket.on('clickOnGraph', function(){
console.log('Reception of the broadcast');
console.log('Event clickOnGraph : OK');
});
When I send an event from the server to the client, it works, but not the opposite...
And $scope.clickOnGraph is working.
Thank you for your help, I'm gettting crazy

Related

What is the purpose of fetch listener in below code for service worker

What is the purpose of fetch in below code.Can i send the request to get some data from the server ?
(function() {
'use strict';
self.addEventListener('install', function(event) {
console.log('Service worker installing...');
self.skipWaiting();
});
self.addEventListener('activate', function(event) {
console.log('Service worker activating...');
});
self.addEventListener('fetch', function(event) {
console.log('Fetching:', event.request.url);
});
})();
When you are listening for fetch event(using the last function) the event object will contain the url from where you are trying to fetch a response using JS's Fetch API.
In the given piece of code, you are simply logging out the url. Notably, you are registering all the listeners inside an IIFE function.
However, if you want to send back a different response you may use event.respondWith() here you can read more about it.

React native socket io no events being emitted from client

Trying to use socket.io-client with react-native (ios for now), so far connection / receiving server side events from client seems to be working fine. However I can't seem to emit any events from the client?
Client
var socket = io("http://localhost:3000");
socket.on('connect', function(){
socket.on('ping', function(e) {
console.log('Server emitted ping: ' + e);
socket.emit('pong', 'hi server!');
});
socket.on('disconnect', function(){
console.log("disconnect");
});
});
Server(Node.js)
var io = require('socket.io')(server);
io.on('connection', function (socket) {
console.log('connected...');
socket.on('pong', function (data) {
console.log("Hmm?");
console.log(data);
});
setTimeout(function() {
console.log("Saying hello");
socket.emit('ping', { message: 'Hello from server ' + Date.now() });
}, 1000);
});
So from the server side, I see the logs
connected...
Saying hello
And in the client I see "Server emitted ping...", but the pong event doesn't seem to be doing anything? I tried catching all events on the server through solutions mentioned in StackOverflow, but it looked like no event was coming from the client. Any ideas?
Using latest RN version 0.31.
Also seeing this error when I first run the app in Xcode, could it be the reason?:
[warn][tid:main][RCTEventEmitter.m:52] Sending `websocketFailed` with no listeners registered.
please try:
io.sockets.on('connection', function(socket) {
....
})

Best practices for calling intuit.ipp.anywhere.setup()?

This is a question about best practices for making the JavaScript call that generates the standard "Connect to QuickBooks" button (for establishing a connection to QuickBooks Harmony via Intuit's v3 REST API).
If I follow Intuit's example, I would:
Reference https://appcenter.intuit.com/Content/IA/intuit.ipp.anywhere.js in a script tag.
Place the <ipp:connectToIntuit></ipp:connectToIntuit> tagset where I want the "Connect to QuickBooks" button to display
Cross my fingers and hope that intuit.ipp.anywhere.js isn't redirecting to a downtime message, again still exists
Make my call to intuit.ipp.anywhere.setup()
See the "Connect to QuickBooks" button
... which works (for many values of "works"), but feels pretty fragile:
If intuit.ipp.anywhere.js is redirecting to a downtime message (read: not JavaScript) or is otherwise unavailable, I'll get a script error.
If I get a script error (or something else goes wrong with Intuit's copy of the script), there isn't any feedback to the user, just a blank space where the "Connect to QuickBooks" button should be.
To make this all a little more resilient, I'm combining the reference to intuit.ipp.anywhere.js and the call to intuit.ipp.anywhere.setup() into a JQuery .ajax() call:
$.ajax({
url: 'https://appcenter.intuit.com/Content/IA/intuit.ipp.anywhere.js',
type: 'GET',
dataType: 'script',
timeout: 4000,
success: function(response) {
if (typeof intuit !== 'undefined') {
intuit.ipp.anywhere.setup({
menuProxy: 'MYMENUPROXYURL.aspx',
grantUrl: 'MYGRANTURL.aspx'
});
}
},
error: function(x, t, m) {
// show some friendly error message about Intuit downtime
}
});
... which also works (for a few more values of "works"):
My call to setup() is wrapped inside the success handler (and an additional check on the existence of the intuit Object), so I shouldn't get a script error if things go wrong.
If the GET of Intuit's script times out (after 4000ms) or returns something that isn't script, I'll show a friendly error message to the user.
Has anyone else taken a different approach?
And is Intuit back online?
That's similar to how we've handled it. We had wrapped it in jQuery.getScript call, but apparently the .fail handler doesn't work with cross domain script tags. Our solution is as follows:
<script type="text/javascript>
var timeoutID;
timeoutID = window.setTimeout(function () {
$("#ippConnectToIntuit").replaceWith('<p class="error-message">There was a problem communicating with QuickBooks. The service may be down or in heavy use. Try again later.</p>');
}, 5000);
$.getScript("https://appcenter.intuit.com/Content/IA/intuit.ipp.anywhere.js")
.done(function () {
window.clearTimeout(timeoutID);
intuit.ipp.anywhere.setup({
menuProxy: '/path/to/our/menu/proxy',
grantUrl: '/path/to/our/grant/url'
});
});
</script>
<div id="ippConnectToIntuit"><ipp:connecttointuit></ipp:connecttointuit></div>

Socket.io emits duplicate data after browser refresh

I have a page that has a button. When the button is clicked, it gets data through socket.io. However, each time I reload the page, socket.io will send back one extra copy of data from the previous data set. So my data would look good first time the page load (example: abcd). Then reload the page will get back 2n data (abcdabcd), reload the page again I get 3n data (abcdabcdabcd) etc.
How do I avoid duplicated data send back to the client when page reload? Here is my code.
Server Side:
app.get('/test', function(req, res){
// some code...
io.sockets.on("connection", function(socket){
var socketFn = function(data){
socket.emit("trends", {
trends: JSON.parse(redisData)
});
};
socket.on("action", socketFn);
socket.on("disconnect", function(){
socket.removeListener("action", socketFn); // this doesn't work
});
});
res.render('test');
});
Client Side:
var socketOpts = {
"sync disconnect on unload" : true
};
var socket = io.connect("", socketOpts);
socket.on("trends", function(data){
// data received from the node server, so do something with it
});
function action(){
socket.emit("action", {phrase: "some dummy data"});
return false;
}
// already checked client side doesn't fire multiple click event
$("button#click").off("click").on("click", action);
That is because you listen to connection as many times as client hits the page.
app.get('/test', function(req, res){
//when client opens the page
io.sockets.on("connection", function(socket){
//start listening to new connection
...
It is considered a bad idea to initialize connection eventlisteners from within your routes. It should be done only once globally. Here each time your client accesses the page it will listen to the events as many times page is accessed.

Backbone Model from Hub in SignalR

How can i create/convert this script into model in Backbone that can use SignaR Hubs? For example:
<script type="text/javascript">
$(function () {
// Proxy created on the fly
var chat = $.connection.chat;
// Declare a function on the chat hub so the server can invoke it
chat.addMessage = function (message) {
alert("message");
};
// Start the connection
$.connection.hub.start();
});
</script>
EDIT
I did come up with this:
window.Message = Backbone.Model.extend({
hub: undefined,
initialize: function () {
this.hub = $.connection.message;
},
addMessage: function (message) {
alert(message);
},
connect: function () {
$.connection.hub.start();
var messages = this.hub.getAll();//get messages
}
});
but this is not working due to the following error:
this error: :55885 Unexpected response code: 200
If you use default settings SignalR will first try to send a websockets poll to the server. The :55885 is simply the port number of your server. Websockets protocol expects a response status code of 101 (see http://dev.w3.org/html5/websockets/).
If running IIS, unless you run Windows 8 with ASP.NET 4.5 your webserver, it will not recognize a web sockets request and (begin speculation) treat it as a normal get request and return status code 200 (OK) (end speculation) which is an unexpected response in the eyes of the websockets initiator. When this happens SignalR falls back to longpolling instead.
This might not answer your question but it will help you understand the error you get (which is likely not the reason why your code doesn't work)
Also, check out http://srtsolutions.github.com/backbone.signalr/ which is a Backbone.js/SignalR integration Nuget package.

Resources