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) {
....
})
Related
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.
I'm trying to use Notification Hub to push Cordova app(iOS)
Azure side is as below.
Source code on client side is as below.
I'm sure Azure client is correctly connected and registration is successful.
function initPushNotification(){
var push = PushNotification.init({
android: {
senderID: "12345679"
},
ios: {
alert: "true",
badge: "true",
sound: "true"
},
windows: {}
});
var registrationSuccess = function () {
alert('Registered with Azure!');
};
var registrationFailure = function (error) {
alert('Failed registering with Azure: ' + error);
};
push.on('registration', function(data) {
client.push.apns.registerTemplate(handle,
'myTemplate', template, null)
.done(registrationSuccess, registrationFailure);
});
push.on('notification', function(data) {
alert('Push Received: ' + data.message);
});
push.on('error', function(e) {
alert(e.message);
});
But when I execute Test Send from notification hub page, nothing happens.
I tried from simple ruby script to APNS directly and notification comes to iPhone correctly.
Does anyone know how to fix it or any information?
My environment is
MacBook Pro
OS X ElCapitan
Cordova 6.0.0
com.microsoft.azure-mobile-services 1.2.9 "Windows Azure Mobile Services"
phonegap-plugin-push 1.6.2 "PushPlugin"
Most likely the call to client.push.register() is not succeeding for some reason. I'm not using the particular plugin you're using, I'm using azure-mobile-apps-cordova-client combined with phonegap-plugin-push. So far, this combination is working for my purposes.
You can find a more complete example here: Add Push Notifications to your Apache Cordova App.
One thing I would add is that when you call the push.register() API in the azure-mobile-apps-cordova-client plugin, you can give it an error function callback that gets called if the API call fails. It would look like this:
push.register('apns', data.registrationId, null, null, function(err)
{console.log(err);});
Lastly, in Visual Studio, you can also connect to your notification hub and list and manage the registrations. This is useful to determine if the APNS registration is really accepted.
I have an issue related to socket.io on its nodejs server. Issue is something related to socket.io is not working on IOS mobile browser in IOS8+. I have searched for it and went through some solutions but nothing worked.
Here are the issue links:
https://github.com/Automattic/socket.io/issues/976
http://www.codedisqus.com/7yzSqUgqge/socketio-working-on-desktop-safari-chrome-but-not-mobile.html
Here is the code
io.sockets.on('connection', function(socket) {
process.emit('client-connection', socket.id);
socket.on('authenticate', function(message) {
console.log('Authenticating client with key "' + message.authToken + '"');
authenticateClient(socket, message);
});
socket.on('message', function(message) {
if (io.sockets.sockets[socket.id] && message.hasOwnProperty('type')) {
if (message.hasOwnProperty('channel')) {
if (settings.clientsCanWriteToChannels || ChannelIsClientWritable(message.channel)) {
process.emit('client-message', socket.id, message);
}
}
return;
}
});
socket.on('disconnect', function () {
console.log("in disconnect");
process.emit('client-disconnect', socket.id);
cleanupSocket(socket);
});
})
Please let me know.
The basic issue is that when I try to open a session on browser it works fine and if I try to do the same on iPhone device browser it fails to work.
As per requirement the issue was IOS socket was not opening over SSL in mobile browsers. I have reinstalled the SSL certificate with complete code packet and it worked for me.
There might be some issue with SSL code packet due to which configuration was not complete.
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
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.