Markydtt
Markydtt

Reputation: 3

Including Socket.io on the client-side

I've been spending all day working on my first app incorporating socket.io with Node and Express and I cannot seem to locate my socket.io.js file on the client side. I've read many SO questions and other forums about this issue, but can't seem to get mine to work with any of the recommended solutions.

I have installed express and socket.io successfully and everything is working fine on the server side (currently just got it working with the Twitter Stream Api). Now that I want to use the data from emit on the client side I need to include my socket file.

I've got HoganJS setup as my view engine.

Here is an excerpt from my app.js file:

app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hjs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(require('less-middleware')({ src: path.join(__dirname, 'public') }));
app.use(express.static(path.join(__dirname, 'public')));

I have another file called index.js which is where my twitter and socket code is,here is my require lines:

var twitter = require('ntwitter');
var io = require('socket.io').listen(3001, {log: false});
// all my twitter setup/functions etc here
io.sockets.emit('tweets' ,data);

Now on the client side, I've tried every possible path I can think of including:

<script src="localhost:3000/socket.io/socket.io.js"></script>
<script src="socket.io/socket.io.js"></script>

And many other paths trying to figure out how to link it in properly. I'm terribly sorry if this is obvious or answered elsewhere, but I couldn't find it.

Any path I can come up with results in a 404 according to chrome dev tools.

Thanks so much!

Upvotes: 0

Views: 2759

Answers (2)

Vardan
Vardan

Reputation: 454

Check whether you have file socket.io.js or not.

check under node_modules -> socket.io -> socket.io.js

If yes then try this path

< script src="/socket.io/socket.io.js" > < /script>

Upvotes: 0

robertklep
robertklep

Reputation: 203241

Since you're running socket.io on port 3001:

<script src="http://localhost:3001/socket.io/socket.io.js"></script>

But you can also run socket.io and Express on the same port together:

var express = require('express');
var app     = express();
var server  = app.listen(3000);
var io      = require('socket.io').listen(server);

Upvotes: 1

Related Questions