While working with localhost I used the following code to implement socket functionality-
Server Side
var express = require('express');
var app = express();
const io = require('socket.io')(5000);
Client Side
<script src="http://localhost:5000/socket.io/socket.io.js"></script>
<script>const socket = io.connect('http://localhost:5000');</script>
The above code worked fine for me locally. Earlier tried use the code given in the official documentation, but it did not work. I got a 404 error when trying to load socket.io.js file and it said 'io is not defined' on the client side. That code is given below-
Server Side
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
Client Side
<script src="/socket.io/socket.io.js"></script>
<script>var socket = io.connect('http://localhost');</script>
Now I need to deploy the app to Heroku and don't know how to do the socket functionality. I tried both the above ways but none worked in Heroku. I am getting '404 error' and 'io is not defined'. The server-side code is written in ./app.js and client-side code written in ./public/index.html.
How do I get this socket functionality to work in Heroku and also why is the official code not even working locally?
Trying to get the same thing working, don't have all the answers but on heroku you need to grab the port from the environment variable.
// Needed for Heroku serving
if (process.env.PORT > 0)
port = process.env.PORT;
else
port = 5000;
// create the express server (app) and the socket io server (io)
const app = express();
const server = app.listen(port, () => {
console.log("Listening on port: " + port);
});
const io = require('socket.io')(server);
Client Side
const socket = socketIOClient('APPNAME.herokuapp.com:80');
socket.emit("MsgType", "Payload")
Related
So I have this web application that is am moving to my personal website but I've been encountering a few errors and I haven't been able to find a solution for this one:
For some reason I get
polling.js:311 GET https://kayden.dev/socket.io/?EIO=4&transport=polling&t=O35Ebas 404
on the client side that appears every few seconds.
My code does need some cleaning up so I won't be showing everything but here. The server side code looks something like this:
const http = require('http');
const express = require('express');
const socket = require('socket.io');
const path = require('path');
let app = express();
let port = app.get('port');
let users = [];
let amountOnline = 0;
//send static files from public to the client
let server = app.listen(port, function(){
console.log('listening to requests on port '+ port);
});
//first parameter is the /whatever that goes after the url
app.use("/skyfighters", express.static(__dirname + '/public/'));
//setup socket
let io = socket(server);
io.serveClient(true);
And the client side code only has let socket = io.connect();
that actually executes at the moment with this error happening
Any help is appreciated, this is the first time I migrate a node app the a web server so I'm quite unfamiliar with it.
Thanks!
I am using Socket.io in my NodeJS backend. However, the sockets do not work. For example, one should receive a link and then send them to all other rooms under the same code, but the code is not executing. In my heroku logs I receive no errors but when I inspect element the page I get
polling-xhr.js:268 GET https://localhost:5000/socket.io/?EIO=3&transport=polling&t=NDADDNH net::ERR_CONNECTION_REFUSED
and
Failed to load resource: net::ERR_CONNECTION_REFUSED
I have looked into similar problems from this forum and made several changes but none of them have solved the issue. Also a bunch of the posts answer with solutions for ws in general which I don't understand at all :/
From what I read the issue might be with my ports? I followed a few of them but the same errors still occured.
Socket.io:
/***BACKEND***/
const express = require('express');
const path = require('path');
const app = express();
let rooms = [];
/***SERVER***/
const port = process.env.PORT || 5000;
server = app.listen(port, function(){
console.log('App is listening on port ' + port)
});
/***SOCKET.IO***/
const socket = require('socket.io');
io = socket(server);
io.on('connection', (socket) => {
//bunch of functionality
}
and then in my client I am using
this.socket = io('localhost:5000');
//one of the functions
this.syncQueue = () => {
this.socket.emit('SYNC_QUEUE', {
activeRoom: this.props.getRoom()
});
}
this.socket.on('RECEIVE_QUEUE', newQueue => {
props.onAddToQueue(newQueue);
});
FYI Everything works on localhost
Localhost will not work on the server and if you are using default namespace you no need to specify the URL. So try this, this.socket = io()
On the client side, you're trying to connect to localhost:5000 instead of the URL Heroku provides. Try this this.socket = io('heroku url goes here').
I'm pretty new to NodeJS and trying to get Socket.IO running with an Express application. I've tried to stick to the docs and tutorials as close as possible, but in the end, socket.io.js cannot be found on client side.
The server is being started as follows:
var app = require('../app');
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() { debug('...') } );
Routing/serving pages:
var express = require('express');
var router = express.Router();
router.get('/', function (req, res) {
res.render('index', { title: 'Express' });
});
Including socket.io:
var express = require('express');
var http = require('http').Server(express);
var io = require('socket.io')(http);
And in the HTML file, for clients:
<script src="/socket.io/socket.io.js" type="text/javascript">
But when loading the page, this JS-file is never found. I suspect that including socket.io via that previously created http server is somehow wrong... Or am I missing something else?
Update
I just found out that the snipped that includes socket.io is executed before the server is started (first snippet). They're in different files and the server-start one (named www.js) is configured as "start" script in package.json, the other one (websockets.js) required indirectly by that.
If I put
var io = require('socket.io')(server);
at the end of www.js, everything works just fine. But is there a way to do it from websockets.js, which is loaded before, e.g. by a server start callback function?
Try specifying the full path, like this
<script src="http://localhost:8000/socket.io/socket.io.js"></script>
And connect to your server from client side:
var socket = io.connect('http://localhost:8000/');
also check if socket.io module is available in your node_modules directory.
Can't get socket.io to work and always got error ReferenceError: io is not defined
Server code:
express = require('express');
var socket = require('socket.io')
app = express();
var io = socket.listen(app);
app.get('/room', function (req, res) {
res.render('room.ejs')
});
io.sockets.on('connection', function (client) {
console.log('Client connected...')
});
app.listen(process.env.PORT || 17336);
Client code:
<script src="localhost:17336/socket.io/socket.io.js"></script>
<script>var socket = io.connect('http://localhost:17336')</script>
Here's directory structure of my site:
(making it in WebMatrix)
Site
|__iisnode
|__node_modules
|__.bin
|__ejs
|__express
|__socket.io
|__public
|__views
|__room.ejs (Client code is here)
|__favicon.ico
|__robots.txt
|__server.js (Server code is here)
|__web.config
This is because localhost:17336/socket.io/socket.io.js is not available. I am guessing your socket.io server is not starting because you are using Express 2.0 syntax, but have Express 3.0 installed.
Express 3 requires that you instantiate a http.Server to attach socket.io to first. Here is how I do it :
var express = require('express');
var app = express()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server);
I don't know much about socket.io, but taking the error message literally, it appears that localhost:17336/socket.io/socket.io.js is not importing the symbol io into the client. Then the next line is impossible because io is not yet defined.
You should open that socket.io.js file in your browser and read it, to make sure it really is defining a variable called io.
i've read the answers here about my problem in node.js, but my problem persists. With socket.io in the server, everything looks ok, but in client side, chrome says:
Failed to load resource: the server responded with a status of 404 (Not Found)
localhost:3382/socket.io/socket.io.js
var express = require("express");
var http = require("http");
var _io = require("socket.io");
var PORT = 3382;
var app = express();
app.listen(PORT, function(){
console.log("Listening from "+PORT);
});
var server = http.createServer(app);
var io = _io.listen(server);
With jade i have:
script(src='/js/jquery.js')
script(src='/js/chat.js')
script(src="socket.io/socket.io.js")
I have the folder js in a folder static, it works ok. I don't know if it's because the versions (node -v show v0.8.16), how do i see versions of express and socket.io?
Read through http://expressjs.com/api.html#app.listen, note how app.listen is defined.
In short: replace app.listen with server.listen.