angular-fullstack / generator-angular-fullstack

Yeoman generator for an Angular app with an Express server
https://awk34.gitbook.io/generator-angular-fullstack
6.12k stars 1.24k forks source link

integrate socket.io wont work on client side #180

Closed dotob closed 10 years ago

dotob commented 10 years ago

after a vanilla setup with fullstack i want to integrate socket.io. server side works fine. but can you explain how i have to setup clientside delivery of socket.io.js? i think something with routes is messed up.

when i do

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

on the clientside side express delivers index.html instead of socket.io.js.

why?

DaftMonk commented 10 years ago

I ran into this when adding socket.io into one of my apps. I think it was because socket.io wasn't being initialized with the same server that is used for the express app. This was causing it to not to serve the socket.io.js file.

To explain the reason why it delivers the index.html page rather than a 404, see this answer: https://github.com/DaftMonk/generator-angular-fullstack/issues/173#issuecomment-40306594

Here's an example of a working configuration: https://github.com/DaftMonk/mean-chat/blob/master/server.js

dotob commented 10 years ago

hi @DaftMonk i looked trough mean-chat and it looked the same as my code (at least i didnt find the difference).

i reduced my code (still almost vanilla fullstack) into a simple sample:

'use strict';

var express = require('express');

var app = express();

// socket.io config
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
io.set('log level', 0);

// Socket.io Communication
io.sockets.on('connection', function (socket) {
  console.log(socket);
});

// in fullstack this comes from lib/routes.js
app.get('/*', function(req, res) {
  // in fullstack this comes from lib/controllers/index.js, but is simplified here
  // if i go to http://localhost:300/socket.io/socket.io.js i will arrive here...WHY?? sorry i dont get it
  // if i remove this app.get i get "Cannot GET /socket.io/socket.io.js" in the browser
  console.log("get /*");
});

// Start server
app.listen(3000);

// Expose app
exports = module.exports = app;

this is the package.json:

{
  "name": "socketdummy",
  "version": "0.0.1",
  "dependencies": {
    "express": "^3.4.3",
    "lodash": "^2.4.1",
    "socket.io": "^0.9.16"
  },
  "engines": {
    "node": ">=0.10.0"
  },
  "scripts": {
    "start": "node server.js"
  }
}

but i still wont get socket.io.js.... if you cant look for this perhaps you can give me some hints how i could debug this.

thanks a lot

evgenykochetkov commented 10 years ago

@dotob to fix your sample code you just have to replace

// Start server
app.listen(3000);

with

// Start server
server.listen(3000);
JaKXz commented 10 years ago

@dotob if you still haven't figured this out, you could try the latest [pre-release] version of the generator (2.0.0-rc1 at the time of writing) for a socket.io integration example. @DaftMonk can speak more to the stability of this version.

DaftMonk commented 10 years ago

@JaKXz actually I had some issues with the prerelease process with npm, and won't be able to easily update prereleases until i get that sorted out. I would recommend installing the 2.0.0 version of the generator by uninstalling the current fullstack generator, cloning the repo and switching to the 2.0.0 branch, then running npm link. Until 2.0.0 is released, you can keep it updated by pulling and running npm install again.

As far as stability, it will be a little bit before I feel confident in it, and I'm sure right now there are some issues. Pretty much everything is working, but there's going to be some refactoring, so its still unstable. The main thing left to do is to get everything working correctly with the ng-component generator for scaffolding additional front-end directives/services/controllers.

JaKXz commented 10 years ago

Closing with 2.0