Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Socket.io connection only establishes on server reset (nodejitsu) works fine locally?

Socket.io connection only establishes on server reset (nodejitsu) works fine locally?

Problem

So basically I'm making a simple RTS board game with node.js/express/socket.io for a learning experience. So far it's going really well and I though I was almost there working purely on the localhost. Deployed it to nodejitsu and loaded the page up, but the server isn't sending the initial server data to the client. If (while the page is open, no refresh) I reset the server it then establishes connection and the whole thing works as expected, but a client visiting the server doesn't do it.

Server init code:

    var express = require('express');
var app = require('express')()
  , server = require('http').createServer(app)
  , io = require('socket.io').listen(server);


server.listen(8080);

app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res){

    res.sendfile("index.html");

});

app.get('/RTSwords', function(req, res){

    //res.sendfile("public\\RTSwords.html");
    res.sendfile("RTSwords.html");

});

Server socket.io code

// define interactions with client
io.sockets.on('connection', function(socket){
    //send them board and first seven tiles.

    socket.emit('serverNews', {'game': JSON.stringify(gameBoard), 'newTiles': randomTileString(7)});

And client socket.io code

var socket = io.connect('http://MYURLHERE');
.......
socket.on('serverNews', function(data){
            update(data);
        });

So yeah,in summary it works locally, works when page is loaded and server is restarted (I'm just "jistu deploy"ing to do that) but not when server is running then page is loaded.

Any help would be much appreciated!

Thanks,

Problem courtesy of: tomatosource

Solution

On the client use io.connect() , if you need more help join #nodejitsu on IRC, http://webchat.jit.su or irc://chat.freenode.net/#nodejitsu

Solution courtesy of: blakmatrix

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Socket.io connection only establishes on server reset (nodejitsu) works fine locally?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×