Can't connect to server with socket.io on both client and server - ios

I just started building me server using socket.io for both my client and Node.js server side.
I'm writing an Objective-c project so i walk through the process of adjusting my project to use Swift alongside with Objective-c which was a pain but it seems to be ok now.
The thing is, when i try to do a simple connect to my server, which prints to log on each connection, nothing happens.
This is the code for the server (Taken from here):
var fs = require('fs')
, http = require('http')
, socketio = require('socket.io');
var server = http.createServer(function(req, res) {
res.writeHead(200, { 'Content-type': 'text/html'});
}).listen(8080, function() {
console.log('Listening at: http://localhost:8080');
});
socketio.listen(server).on('connection', function (socket) {
console.log('Connected');
socket.on('message', function (msg) {
console.log('Message Received: ', msg);
socket.broadcast.emit('message', msg);
});
});
Super simple, really nothing to it.
And the Objective-c code for my client which is even more simple:
- (void) connect
{
SocketIOClient* client = [[SocketIOClient alloc]initWithSocketURL:#"http://127.0.0.1:8080/" options:nil];
[client connect];
}
But i can't see nothing on my console except the Listening at: http://localhost:8080 message.
I can't seem to find what i'm doing wrong here, and the fact that the swift debugging is horrible in this combined project, i can't really fully go through the socket.io debugging myself (but i'm pretty sure nothing's wrong with their code)
Any help would be much appreciated.

plz confirm, if you are using the simulator for iOS testing. For device, you need to assign public address to your NodeJS server, and then need to use it's ip in iOS Codebase.
I had used https://github.com/pkyeck/socket.IO-objc during my last project and it worked like a charm.
For simulator, your code should ideally work.

Related

Ktor-websocket library do nothing when trying to receive data on client

I am currently trying to connect our Kotlin Multiplatform Project to websockets. I would like to use ktor-websockets library to receive some updates from our backend but onfortunately when I run this code, nothing happens:
client.webSocket(
port = 80,
method = HttpMethod.Get,
host = "https://uat-betws.sts.pl",
path = "/ticket?token=eyJzdWIiOiI0ZmY5Y2E1Mi02ZmEwLTRiYWYtODlhYS0wODM1NGE2MTU0YjYiLCJpYXQiOjE2MTk4MDAwNzgsImV4cCI6MTYxOTgwMzY3OH0.oIaXH-nFDpMklp4FSJWMtsM7ECSIfuNF99tTQxiEALM"
)
{
for (message in incoming) {
message as? Frame.Text ?: continue
val receivedText = message.readText()
println(receivedText)
}
// Consume all incoming websockets on this url
this.incoming.consumeAsFlow().collect {
logger.d("Received ticket status websocket of type ${it.frameType.name}")
if (it is Frame.Text) {
Json.decodeFromString<TicketStatusResponse>(it.readText())
}
}
}
Does somebody have any experience with ktor-websockets library? There is almost no documentation so maybe I am doing something wrong.
Thank you
As the documentation says
Ktor provides Websocket client support for the following engines: CIO, OkHttp, Js.
This means that it works only on JVM/JS, you're probably targeting iOS. It's not yet supported, you can follow issue KTOR-363 for updates
For sure the team is working on it, but for now you had to implement it by yourself using expect/actual, you can check out official example
An other possible problem in your code: host shouldn't include https://, if you're using ssl, you should add an other parameter:
request = {
url.protocol = URLProtocol.WSS
}
Or use client.wss(...) - which is just a short form for the same operation

MQTT on Parse platform (Open source parse Server)

Hi I am using the hosted version of the open source parse platform (hosed version on Back4app) for my IoT project. Am using HTTP (REST) Api to communicate with the parse server and upload data. Does anyone know if it is possible to use the MQTT protocol instead of HTTP for the same with the parseplatform. I couldn't find any relevant doc for this. Apparently there's a way to install the MQTTjs on cloud code section of the platform but do not know if this really works ... Thanks in advance
Yes, it's possible, I just tested it now and it worked for me. Here are the steps that you need to follow:
1 - You only need to install this npm module as you can see at this guide.
Here is my package.json:
{
"dependencies": {
"mqtt": "2.18.8"
}
}
2 - After that, on Back4app, you need to upload the code in your cloud code and check your Server System Logs at Server Settings > Logs > Settings.
Here's a simple code that you can use to test it. I put this code in my main.js:
var mqtt = require('mqtt')
var client = mqtt.connect('mqtt://test.mosquitto.org')
client.on('connect', function () {
client.subscribe('presence', function (err) {
if (!err) {
client.publish('presence', 'Hello mqtt')
}
})
})
client.on('message', function (topic, message) {
// message is Buffer
console.log(message.toString())
client.end()
});

Unable to replicate database in pouchDB

I'm trying a react native application using couchDB 2.1.1. PouchDB entry in package json looks like this:
"pouchdb": "^6.3.4",
"pouchdb-react-native": "^6.3.4",
Replication is as shown below:
const localDB = new PouchDB('employee');
const remoteDB = new PouchDB('http://username:password#localhost:5984/employee');
localDB.replicate.from(
remoteDB,
(err, response) => {
if (err) {
return console.log(err);
}
},
);
I get following error:
{"code":"ETIMEDOUT","status":0,"result":{"ok":false,"start_time":"...","docs_read":0,"docs_written":0,"doc_write_failures":0,"errors":[],"status":"aborting","end_time":"...","last_seq":0}}
Almost all the times this works fine when I run the app in debug mode. Tried ajax timeout as shown here PouchDB ETIMEDOUT error. This didn't work. Is there something that I'm supposed to look in my code? Please help.
Had the same issue, the following fixed it for me:
Use your PC ip address instead of localhost
Configure your firewall
to allow connections on port 5984 OR just disable it (Not
recommended)

Nodejs Socket.io working on desktop safari & chrome, but not iPhone

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.

Socket.io-1.0 get clients from namespace

I work with socket.io 1.0 and maybe I'm wrong with my conception.
Actually, I open a namespace server side with
var nsp = io.of('/myNamespace');
And clients connect with
var socket = io.connect('http://localhost/myNamespace');
I can start communication without problems.
Server side I catch signals with
nsp.on('connection', function(socket){
socket.on('disconnect', function(){
//problem here
});
});
In the disconnect I would like to disconnect all sockets connected to my namespace, so i tried to do
for(var myParticipantID in io.sockets.adapter.nsp.connected)
{
io.sockets.adapter.nsp.connected[myParticipantID].disconnect();
}
but it doesn't work ... I don't have error but clients still connecting
I tried with
io.sockets.nsp.clients();
but I have error since socket.io 1.0
I don't want to create room, but maybe it's my mistake?
Thanks for your help,
MagicDenver
If it would help somebody,
I work with node js so I created a value:
app.set(idNameSpace,[]);
and push socket when I have a new connection
You should use the io.of(namespace) function to get connected clients.
for (var id in io.of('/namespace').connected) {
var s = io.of('/namespace').connected[id];
s.disconnect();
}
If you don't know the namespace and you are in a socket.on statement, you can use socket.nsp.connected instead of io.of('/namespace').connected

Resources