SocketIO can't emit to room - node.js

Whenever I try to emit to room I get this error message on my server:
TypeError: Object #<Manager> has no method 'in'
at Query.<anonymous> (/root/server.js:553:19)
at /root/node_modules/mongoose/node_modules/kareem/index.js:177:19
at /root/node_modules/mongoose/node_modules/kareem/index.js:109:16
at process._tickCallback (node.js:419:13)
My code:
var socketio = require('socket.io');
var router = express();
var server = http.createServer(router);
var io = socketio.listen(server);
io.in(room).emit("inGame",Date.now()+10000);`

Okay so I found the problem. for any people who get this problem in the future
make sure you use the latest version of socket.io. before updating check your package.json to see if socketio is written to install a older version remove r update the version in the package.json.

Related

React not recognizing Express

In my jsx file, I have the following:
var express = require('express');
var myExpress = express();
var http = require('http');
var app = http.createServer(myExpress);
var { Server } = require("socket.io");
var myio = new Server(app);
However, the browser says "Uncaught TypeError: express is not a function"
I have tried importing express with an import statement, as well as making my project a module in my package.json. What is weird is that when I use the same code in a regular js file in the same folder, it works perfectly well. This code was the code in every single one of the tutorials, so I am at a loss. Thank you.
Express is node framework you can't use it in react.
I think you need https://v5.reactrouter.com/web/guides/quick-start

Error on creating socket server with socket.io on typescript

Im new at socket io and im trying to use typescript on the project so i got the error of the image on start the socket server, i already tried to change the import to import * as socketIo from 'socket.io' but nothing changes, i dont know if is some version issue or im doing something wrong.
the error:
code:
Type declaration for socket.io does not contain default export. You can check it in its index.d.ts. It contains the following:
export { Socket, ServerOptions, Namespace };
That's why to init socket.io server object you need to use the following instruction:
const app = express();
const httpServer = new http.Server(app);
const io = new socketio.Server(httpServer);

Not Found error for socket.io on the client-side

I am trying to include socket.io on the client-side. I keep getting this error message in the console every 5 seconds:
GET https://example.com/socket.io/?EIO=3&transport=polling&t=Lo8ssW0 404 (Not Found)
My code:
script(src='https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.2/socket.io.slim.js')
var socketio = io.connect('https://example.com',{secure: true, port:5089})
On the server-side I have:
const socketio = app.listen(5089)
var io = require('socket.io')(socketio)
which works correctly.
What am I doing wrong?
Simply replace
var socketio = io.connect('https://example.com',{secure: true, port:5089})
by:
var socketio = io.connect()
Does this help? If so, the problem is the host or the port in your original function call.
This simplified code should work, at least if you load the client from the server that runs Socket.io as well.

how to resolve following easyrtc issue?

I've written following code:
var http = require("http"); // http server core module
var express = require("express"); // web framework external module
var io = require("socket.io"); // web socket external module
var easyrtc = require("easyrtc"); // EasyRTC external module
var app = express();
var server = http.createServer(app).listen(app.get('port'));
io= io.listen(server,{"log level":1});
var rtc = easyrtc.listen(server, io);
this is giving following error:
$node server info - EasyRTC: Starting EasyRTC Server (v1.0.10) on
Node (v0.10.26) [TypeError: Object # has no method 'get']
TypeError: Object # has no method 'get'
at async.waterfall.pub.socketServer.sockets.on.easyrtcid (/home/ritzy1/Downloads/downloaded
codes/VEDIO/change2/testexpandwb/node_modules/easyrtc/lib/easyrtc_default_event_listeners.js:1472:29)
at fn (/home/ritzy1/Downloads/downloaded codes/VEDIO/change2/testexpandwb/node_modules/easyrtc/node_modules/async/lib/async.js:582:34)
at Object._onImmediate (/home/ritzy1/Downloads/downloaded codes/VEDIO/change2/testexpandwb/node_modules/easyrtc/node_modules/async/lib/async.js:498:34)
at processImmediate [as _immediateCallback] (timers.js:330:15)
Ηow can i fix it?
You have to give a specific port number in the listen() function.
Change the following line var server = http.createServer(app).listen(app.get('port')); with var server = http.createServer(app).listen(8080); Note : 8080 is a port number you can specify any other port number which are not currently using.
Happy coding

unable to execute server.js program using express framework on node.js

While trying to execute server.js program I am getting the following error:
var app = express();
Type error: object is not a function
at object.<anonymous>
even tried re installing and changing the version of express to
npm install
npm uninstall express
npm install express#2.5.9
but it resulted in new error
fqdn = ~req.url.indexof(' ://')
I use windows and i am working on node.js version 0.8.4.
If you're using Express < 3.0, the return value of require('express'); is not a function; you'll need to create a server the old way.
Express 2.x
var express = require('express');
var server = express.createServer();
Express 3.x
var http = require('http');
var express = require('express');
var app = express();
var server = http.createServer(app);
What does
> require('express').version;
'3.0.0rc2'
return?
As you can see it does return 3.0.0rc2? Does yours really return 2.5.9. if it does you use like Brandon said 2.x section. If it returns 3.x you use his 3.x section.

Resources