NodeJS Server Not Receiving Socket.emit() from Swift Client - node.js

I am trying to put together a very simply multiplayer tic-tac-toe game in swift with a NodeJS backend. When I try and do socket.emit() from my swift client, however, the server does not recognize it.
Client Code:
SocketIOManager:
import UIKit
class SocketIOManager: NSObject {
static let sharedInstance = SocketIOManager()
override init() {
super.init()
}
var socket: SocketIOClient = SocketIOClient(socketURL: NSURL(string: "http://10.0.1.30:2000")! as URL)
func connectToServer(completionHandler: #escaping (_ userList: [[String: AnyObject]]?) -> Void) {
socket.emit("connectUser")
socket.on("userList") { ( dataArray, ack) -> Void in
completionHandler(_: dataArray[0] as? [[String: AnyObject]])
}
}
func establishConnection() {
socket.connect()
}
func closeConnection() {
socket.disconnect()
}
}
Game Scene:
import SpriteKit
class GameScene: SKScene {
let screenSize = UIScreen.main.bounds
var board = SKSpriteNode(imageNamed: "Board.png")
var users = [[String: AnyObject]]()
override func didMove(to view: SKView) {
SocketIOManager.sharedInstance.connectToServer(completionHandler: { (userList) -> Void in
DispatchQueue.main.async(execute: { () -> Void in
if userList != nil {
self.users = userList!
}
})
})
board.size = CGSize(width: screenSize.width * 2/3, height: screenSize.width * 2/3)
board.position = CGPoint(x: screenSize.width/2, y: screenSize.height/2)
self.backgroundColor = UIColor.white
self.addChild(board)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func update(_ currentTime: TimeInterval) {
}
}
Server Code:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var userList = [];
app.get('/', function(req, res){
res.send('<h1>Tic-Tac-Toe Server</h1>');
});
http.listen(2000, function(){
console.log('Listening on *:2000');
});
io.on('connection', function(clientSocket) {
console.log('a user connected');
clientSocket.on('disconnect', function() {
console.log('user disconnected');
});
clientSocket.on('connectUser', function() {
console.log('User with id ' + clientSocket.id + ' connected');
var userInfo = {};
var foundUser = false;
for (var i = 0; i < userList.length; i++) {
if (userList[i]["id"] == clientSocket.id) {
userInfo = userList[i];
foundUser = true;
break;
}
}
if (!foundUser) {
userInfo["id"] = clientSocket.id;
userList.push(userInfo);
}
io.emit("userList", userList);
io.emit("userConnectUpdate", userInfo);
});
});
The message in particular that is not working is the "connectUser" one, but I have tried to create others to test it and none of them work. It appears as if the server never receives them.

Since you are not using a https (http://10.0.1.30:2000), probably you forgot to set Allow Arbitrary Loads to YES in your Info.plist file at your iOS project:
Anyways I've built a basic example that's working:
Server side:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = 5000;
io.on('connection', function(socket) {
socket.on('message', function(data) {
console.log('client sent a message: ' + data);
});
});
http.listen(port, function() {
console.log('server up and running at %s port', port);
});
Client side:
import UIKit
import SocketIO
class ViewController: UIViewController {
let socket = SocketIOClient(socketURL: URL(string: "http://localhost:5000")!)
override func viewDidLoad() {
super.viewDidLoad()
socket.on("connect") { data, ack in
print("socket connected")
self.socket.emit("message", "Hello dear server from iOS.")
}
socket.connect()
}
}
Logs from Server

Related

Why the module C does not receive the socket io "hi" event?

How to call different module for different socket path?
Here is server code:
require('dotenv-flow').config();
const express = require('express');
const app = express();
const http =require('http');
const httpServer= http.createServer(app);
const io = require('socket.io')(httpServer);
let C=require('./C');
let c=new C(io.of("/c"));
class C {
constructor(socket) {
socket.on("connection",()=>{
console.log("Connection to /c");
});
socket.on("hi", (peerName, calllBack) => {
console.log("Received say hi from " + peerName + ".");
});
}
}
module.exports = C;
The client side code:
import { useEffect} from "react";
import io from "socket.io-client";
export default function TestSocket() {
useEffect(() => {
let peerName;
let signalServerURL=process.env.REACT_APP_SOCKET_URL + "c";
let sUsrAg = navigator.userAgent;
if (sUsrAg.indexOf("Edg") > -1) {
peerName = "Edge";
} else {
if (sUsrAg.indexOf("Chrome") > -1) {
peerName = "Chrome";
} else {
if (sUsrAg.indexOf("Firefox") > -1) {
peerName = "Firefox";
} else {
if (sUsrAg.indexOf("Safari") > -1) {
peerName = "Safari";
}
}
}
}
let socket = io(signalServerURL, {
transports: ["websocket"],
});
socket.emit("hi", peerName, (response) => {
console.log(response);
});
},[]);
return (
<></>
)
}
The module C can receive "connection" event, however, it cannot receive the "hi" event, why?
Besides that, I want to use different module/class to handle different socket path.
e.g.
for the path /b socket access, I want to use the module B to handle it.
for the path /d socket access, I want to use the module D to handle it.
How can I implement it?
According to the Philippe answer, I modified the server code as below:
require('dotenv-flow').config();
const express = require('express');
const app = express();
const http =require('http');
const httpServer= http.createServer(app);
const io = require('socket.io')(httpServer);
let C=require('./C');
let c=new C(io,"/c");
class C {
constructor(io,path) {
io.of(path).on("connection",socket=>{
console.log("Connection to "+path);
socket.on("hi", (peerName, calllBack) => {
console.log("Received say hi from " + peerName + ".");
});
});
}
}
module.exports = C;
It works!
When a connection is successful, socket-io returns the socket you need to use. You can try something like:
constructor(io) {
io.on("connection", (socket) => {
console.log("Connection to /c " + socket.id);
socket.on("hi", (peerName, calllBack) => {
console.log("Received say hi from " + peerName + ".");
});
});
}

New connection cause the current connections to stop working

I am making the chat application using socket (which I'm new at) with multiple tenants structure and using namespaces. Here's my code:
Socket server:
index.js
class Server {
constructor() {
this.port = process.env.PORT || 3000;
this.host = process.env.HOST || `localhost`;
this.app = express();
this.http = http.Server(this.app);
this.rootSocket = socketio(this.http);
}
run() {
new socketEvents(this.rootSocket).socketConfig();
this.app.use(express.static(__dirname + '/uploads'));
this.http.listen(this.port, this.host, () => {
console.log(`Listening on ${this.host}:${this.port}`);
});
}
}
const app = new Server();
app.run();
socket.js
var redis = require('redis');
var redisConnection = {
host: process.env.REDIS_HOST,
password: process.env.REDIS_PASS
};
var sub = redis.createClient(redisConnection);
var pub = redis.createClient(redisConnection);
class Socket {
constructor(rootSocket) {
this.rootIo = rootSocket;
}
socketEvents() {
/**
* Subscribe redis channel
*/
sub.subscribe('visitorBehaviorApiResponse');
//TODO: subscribe channel..
// Listen to redis channel that published from api
sub.on('message', (channel, data) => {
data = JSON.parse(data);
console.log(data);
const io = this.rootIo.of(data.namespace);
if (channel === 'visitorBehaviorApiResponse') {
io.to(data.thread_id).emit('receiveBehavior', data);
io.to('staff_room').emit('incomingBehavior', data);
}
})
sub.on('error', function (error) {
console.log('ERROR ' + error)
})
var clients = 0;
this.rootIo.on('connection', (rootSocket) => {
clients++;
console.log('root:' + rootSocket.id + ' connected (total ' + clients + ' clients connected)');
const ns = rootSocket.handshake['query'].namespace;
// Dynamic namespace for multiple tenants
if (typeof (ns) === 'string') {
const splitedUrl = ns.split("/");
const namespace = splitedUrl[splitedUrl.length - 1];
const nsio = this.rootIo.of(namespace);
this.io = nsio;
this.io.once('connection', (socket) => {
var visitors = [];
console.log('new ' + socket.id + ' connected');
// once a client has connected, we expect to get a ping from them saying what room they want to join
socket.on('createChatRoom', function (data) {
socket.join(data.thread_id);
if (typeof data.is_staff !== 'undefined' && data.is_staff == 1) {
socket.join('staff_room');
} else {
if (visitors.some(e => e.visitor_id === data.visitor_id)) {
visitors.forEach(function (visitor) {
if (visitor.visitor_id === data.visitor_id) {
visitor.socket_ids.push(socket.id);
}
})
} else {
data.socket_ids = [];
data.socket_ids.push(socket.id);
visitors.push(data);
}
socket.join('visitor_room');
}
//TODO: push to redis to check conversation type
});
socket.on('sendMessage', function (data) {
console.log(data);
pub.publish('chatMessage', JSON.stringify(data));
this.io.in(data.thread_id).emit('receiveMessage', data);
this.io.in('staff_room').emit('incomingMessage', data);
// Notify new message in room
data.notify_type = 'default';
socket.to(data.thread_id).emit('receiveNotify', data);
}.bind(this))
socket.on('disconnect', (reason) => {
sub.quit();
console.log('client ' + socket.id + ' left, ' + reason);
});
socket.on('error', (error) => {
console.log(error);
});
});
}
// Root disconnect
rootSocket.on('disconnect', function () {
clients--;
console.log('root:' + rootSocket.id + ' disconnected (total ' + clients + ' clients connected)');
});
});
}
socketConfig() {
this.socketEvents();
}
}
module.exports = Socket;
Client:
const server = 'https://socket-server'
const connect = function (namespace) {
return io.connect(namespace, {
query: 'namespace=' + namespace,
resource: 'socket.io',
transports: ['websocket'],
upgrade: false
})
}
const url_string = window.location.href
const url = new URL(url_string)
const parameters = Object.fromEntries(url.searchParams)
const socket = connect(`${server}/${parameters.shopify_domain}`)
var handleErrors = (err) => {
console.error(err);
}
socket.on('connect_error', err => handleErrors(err))
socket.on('connect_failed', err => handleErrors(err))
socket.on('disconnect', err => handleErrors(err))
The problem that I met is when socket server got a new connection, the existing connections will be stopped working util they make a page refreshing to reconnect a new socket.id.
And when a namespace's client emit data, it sends to other namespaces, seem my code is not work correctly in a namespace.
Could you take a look at my code and point me where I'm wrong?
Thanks
1) Get UserId or accessToken while handshaking(in case of accessToken decrypt it).
and store userID: socketId(in Redis or in local hashmap) depends upon the requirement .
2) When u are going to emit to particular user fetch the socketid to that userid from redis or local hashmap
and emit to it.
**io.to(socketId).emit('hey', 'I just met you');**
3) If you are using multiple servers use sticky sessions
4) Hope this will help you

nodejs can't receive on private channel of socket.io

I am initiating socket.io between nodejs (express), and front end. Basically, from nodejs, I am emitting a broadcast to private channel. Then, I want to receive the call back of this broadcast, on the same channel as well.
This is the setup in server.js:
const port = process.env.PORT || 3000;
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
const api = require('../api/AuthController')
var server = http.listen(3000, () => {
console.log('server is running on port', server.address().port);
});
const routes = require('../api/routes');
routes(app,io);
//socket config
global.io = io; //added
Then in AuthController, I am using global.io to emit, and receive in private channel:
global.io.emit(`news${user._id}`, { hello: 'new request');
global.io.on('conection', function (socket) {
socket.on(`news${user._id}`, function (message) {
console.log('from console', message.value);
});
});
I am able to emit perfectly fine to frontend, but I am not able to receive anything when the front end emits back on the same channel.
Appreciate your help.
Thanks,
Edit
FrontEnd Code:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:adhara_socket_io/adhara_socket_io.dart';
import 'package:geocoder/geocoder.dart';
const String URI = "http://10.0.2.2:3000/";
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
_MyHomePageState();
List<String> toPrint = ["trying to connect"];
SocketIOManager manager;
Map<String, SocketIO> sockets = {};
Map<String, bool> _isProbablyConnected = {};
bool newtripRequest = false;
var pickupController;
var dropoffController;
SocketIO socketController;
var driver = 'driver';
String socketIdentifier;
#override
void initState() {
super.initState();
manager = SocketIOManager();
initSocket("default");
}
#override
void dispose() {
super.dispose();
}
initSocket(String identifier) async {
setState(() => _isProbablyConnected[identifier] = true);
SocketIO socket = await manager.createInstance(SocketOptions(
//Socket IO server URI
URI,
nameSpace: (identifier == "namespaced") ? "/adhara" : "/",
//Query params - can be used for authentication
query: {
"auth": "--SOME AUTH STRING---",
"info": "new connection from adhara-socketio",
"timestamp": DateTime.now().toString()
},
//Enable or disable platform channel logging
enableLogging: false,
transports: [
Transports.WEB_SOCKET /*, Transports.POLLING*/
] //Enable required transport
));
setState(() {
socketIdentifier = identifier;
});
socket.onConnect((data) {
pprint("connected...");
pprint(data);
sendMessage('news', 'yes', socketIdentifier);
});
socket.onConnectError(pprint);
socket.onConnectTimeout(pprint);
socket.onError(pprint);
socket.onDisconnect(pprint);
socket.on("news", (data) => newTripRquest(data));
socket.connect();
sockets[identifier] = socket;
}
bool isProbablyConnected(String identifier) {
return _isProbablyConnected[identifier] ?? false;
}
disconnect(String identifier) async {
await manager.clearInstance(sockets[identifier]);
setState(() => _isProbablyConnected[identifier] = false);
}
sendMessage(privateChannel, messageBody, identifier) {
//pprint("sending message from '$identifier'...");
sockets[identifier].emit(driverChannel, [
{'response' : messageBody}]);
//pprint("Message emitted from '$identifier'...");
}
pprint(data) {
setState(() {
if (data is Map) {
data = json.encode(data);
}
print(data);
toPrint.add(data);
});
}
You got a typo ('conection'), and there's no closing brace in the emit's object.
Here's the fixed version of your AuthController's code:
global.io.emit(`news${user._id}`, { hello: 'new request' });
global.io.on('connection', function (socket) {
socket.on(`news${user._id}`, function (message) {
console.log('from console', message.value);
});
});
I hope it helps!

Node.js stops responding when using serialport

I'm using node.js for the first time with a simple website to control a motor.
On the website are 2 buttons: "Left" and "Right". You have to press and hold the "Left" or "Right" button to turn the motor. When you let the button go the motor stops.
The motor is connected to an Arduino on the serialport on the server.
It works, but
Problem
When I use it on my phone and spam the buttons the server sometimes stops responding for everyone until I restart the server. I thought the functions were called too fast because of the WiFi latency. So I tried calling the functions in the Chrome console.
LeftPressed();LeftReleased();LeftPressed();LeftReleased();
And indeed, after running them a few times the server stops responding.
No error, no crash..
When I remove motorPort.write(message); keeps the server responding.
How can I prevent the server from stopping to respond?
Client (Javascript in browser)
function LeftPressed() {
socket.emit('steer', { left: true });
}
function LeftReleased() {
socket.emit('steer', { left: false });
}
function RightPressed() {
socket.emit('steer', { right: true });
}
function RightReleased() {
socket.emit('steer', { right: false });
}
Server (node.js)
const SerialPort = require("serialport");
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server)
const motorPort = new SerialPort('/dev/arduinoUnoMicroUSB', {
baudrate: 9600,
});
app.use(express.static('web'));
server.listen(8081, function () {
console.log("Server running...");
});
io.on('connection', function (socket) {
socket.on('steer', function (data) {
var message = "2"; //stop
if (data.left != undefined) {
console.log("Left: " + data.left); //log to Console (below)
if (data.left) {
message = "1" //turn left
}
}
if (data.right != undefined) {
console.log("Right: " + data.right);
if (data.right) {
message = "3"; //turn right
}
}
motorPort.write(message); //send message over serial port
});
});
Console output example: "Left" button clicked
Server running...
Left: true //pressed (motor turning left)
Left: false //released (motor stops)
I got it working with this. It's not 100% perfect, but at least the server keeps responding. If you think there is a better way, I am always open for improvements.
const SerialPort = require("serialport");
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const motorPort = new SerialPort('/dev/arduinoUnoMicroUSB', {
baudrate: 9600,
});
app.use(express.static('web'));
server.listen(8081, function () {
console.log("Server running...");
});
var ready = true;
var NotSendMessages = [];
io.on('connection', function (socket) {
socket.on('steer', function (data) {
var message = "2"; //stop
if (data.left != undefined) {
console.log("Left: " + data.left);
if (data.left) {
message = "1" //turn left
}
}
if (data.right != undefined) {
console.log("Right: " + data.right);
if (data.right) {
message = "3"; //turn right
}
}
if (!ready) {
console.log("cats!!!");
NotSendMessages.push(message);
} else {
send(message);
}
});
});
function send(message) {
motorPort.write(message, function () {
ready = false;
motorPort.drain(function () {
motorPort.flush(function () {
ready = true;
});
});
});
}
setInterval(function () {
if (NotSendMessages.length > 0 && ready) {
send(NotSendMessages.shift());
}
}, 50);

How to create online users list using webrtc and nodejs on the server end

I am using webrtc to make a audio, video and chat application where I need keep all the users in a user list in the serverside. Need help how to get this done.
Also, how can I remove users from the list when they logout from the system.
Need help to implement this.
webRTC.rtc.on('connect', function(rtc) {
//Client connected
});
webRTC.rtc.on('send answer', function(rtc) {
//answer sent
});
webRTC.rtc.on('disconnect', function(rtc) {
//Client disconnect
//console.log(webRTC);
});
webRTC.rtc.on('chat_msg', function(data, socket) {
var roomList = webRTC.rtc.rooms[data.room] || [];
for (var i = 0; i < roomList.length; i++) {
var socketId = roomList[i];
if (socketId !== socket.id) {
var soc = webRTC.rtc.getSocket(socketId);
if (soc) {
soc.send(JSON.stringify({
"eventName": "receive_chat_msg",
"data": {
"messages": data.messages,
"id": data.id,
"from": data.from,
"status": data.status,
"email": data.email
}
}), function(error) {
if (error) {
console.log(error);
}
});
}
}
}
});
As I was using webrtc.io module, so below are the methods that helped me to create the userlist and maintain the presence.
webRTC.rtc.on('join_room', function(data, socket) {
// Will get info who joined along with his socket id
}
And
webRTC.rtc.on('room_leave', function(room, socketid) {
// Will get info who left the room
}
Node.js code:
var users = {};
io.sockets.on('connection', function (socket) {
socket.emit('connect', true);
socket.on('message', function (data) {
socket.broadcast.emit('message', data);
});
socket.on('new-user', function (username) {
users[username] = username;
});
socket.on('check-presence', function (username) {
var isUserPresent = !! users[username];
socket.emit('presence', isUserPresent);
});
socket.on('remove-user', function (username) {
var user = users[username];
if (user) delete users[username];
});
});
This may also work (node.js):
var users = {};
io.sockets.on('connection', function (socket) {
var UserName;
socket.emit('connect', true);
socket.on('message', function (data) {
socket.broadcast.emit('message', data);
});
socket.on('new-user', function (username) {
users[username] = username;
UserName = username;
});
socket.on('check-presence', function (username) {
var isUserPresent = !! users[username];
socket.emit('presence', isUserPresent);
});
// removing user on "disconnect"
socket.on('disconnect', function () {
var user = users[UserName];
if (user) delete users[UserName];
});
});
For 1st case; client-side code:
var socket = io.connect();
socket.on('connect', function () {
socket.emit('new-user', 'username');
});
function removeUser() {
socket.emit('remove-user', 'username');
}
window.onbeforeunload = function () {
removeUser();
};
// if someone pressed "F5" key to refresh the page
window.onkeyup = function (e) {
if (e.keyCode == 116)
removeUser();
};
// if someone leaves via <a href>
var anchors = document.querySelectorAll('a'),
length = anchors.length;
for (var i = 0; i < length; i++) {
var a = anchors[i];
if (a.href.indexOf('#') !== 0 && a.getAttribute('target') != '_blank')
a.onclick = function () {
removeUser();
};
}
For 2nd case; client side code:
var socket = io.connect();
socket.on('connect', function () {
socket.emit('new-user', 'username');
});
You can check presence too:
socket.on('presence', isUserPresent) {
// boolean: user is present or not
});
socket.emit('check-presence', 'username');

Resources