Error on creating socket server with socket.io on typescript - node.js

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);

Related

I'm trying to access socket.io in an express-generator app, but getting a null object

I've built a web app with express generator. I'd like to be able to send updates to the browser from a number of different modules.
However, I'm not quite sure why the solution I've put together, below, doesn't work.
I know that the exports.initialize() function is being called, through logging to the console, but the error I'm getting, cannot read property 'emit' of null suggests that socket.io is not being initialized, because it's returning a null object.
I've created a module containing the below wrapper:
const sio = require('socket.io')
let io = null
exports.io = function () {
return io
}
exports.initialize = function(server) {
console.log("init")
return io = sio(server)
}
I've initialised the module in my app.js file, like so:
// Create the http server
const server = require('http').createServer(app);
// Create the Socket IO server on
// the top of http server
const io = require('./app_modules/sockets').initialize(server)
and then, in modules where I need to send information to the browser, I'm calling the sockets module like this:
const io = require('./sockets').io()
Can anyone help me understand why this isn't working, and what I can do to fix it?
In order to start a Socket.IO server, you need to invoke the Server constructor. The io constructor is only for the client side.
Your sockets module should look like this:
const sio = require('socket.io')
let io = null
exports.io = io
exports.initialize = function(server) {
io = new sio.Server(server);
return io
}
And in modules where you need to send information to the clients, you should use something like this:
const io = require('./sockets').io

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

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.

SocketIO can't emit to room

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.

How to use exec-php(node js) in MVC framework like zend?

Basically what I want to do over here is to get data into node server js, data which is returned from a function which is written into a Zend model.
My node js server code is as below:
var socket = require( 'socket.io' );
var express = require( 'express' );
var http = require( 'http' );
var execPhp = require('exec-php');
var app = express();
var server = http.createServer( app );
var io = socket.listen( server );
execPhp('../application/modules/front/models/Dbtable/Postcontent.php',
function(error, php, outprint){
php.fetchEntryAll(function(error, result){
io.sockets.emit('showfeeds', result);
});
});
server.listen( 7000 );
so when I tried to execute above code it gives me error like below:
Error: Command failed: PHP Fatal error: Class 'Zend_Db_Table_Abstract' not found in /Data/www/XXX/application/modules/front/models/Dbtable/Postcontent.php on line 3
same code works when I use core PHP instead of Zend.
So I believe that we can't use OOP stuff with exec-php.
Is there any solution here?
Reference link to exec-php:
https://www.npmjs.com/package/exec-php
The problem you ran into is not about OOP but autoloading your object class file(s).
While you have a path defined to your first file some or all of the following files don't have a path defined for their dependencies--like an Abstract class.
ZF1 files should actually have require statements and work without an auto loader. What you are missing and need is the same in your own modules files.
In your application/modules/front/models/Dbtable/Postcontent.php file you have to hint the path to the Abstract class as a require_once statement.

Resources