George
George

Reputation: 7317

Receiving a client variable from another server using socket.io

Overview

I'm working with two entities: (i) an IRC webapp and (ii) an IRC bot. There is a bot variable botVariable that I'm interested in passing over to the IRC webapp client. I've tried to use socket.io to do this and failed. To me, what makes this problem interesting is that I'm trying to pass a value not from the IRC webapp server to the webapp client, but from the bot server to the webapp client. This is the first time I've used socket.io. Both the bot and the irc webapp are hosted on (different) heroku urls.

Bot code (server-side)

For the bot, I have the following code:

var botVariable = "bot string"; //botVariable is global.

var io = require('socket.io')(HTTPS);
io.on('connection', function(botVariable) {
    //When client connects for the first time, send him the value immediately
    socket.emit('new_value', botVariable);
    console.log(botVariable);
});

console.log("End of bot file.");

IRC code (client-side)

For the IRC-client code, I have the following:

<head>
<script src="https://bot.herokuapp.com/socket.io/socket.io.js"></script>
</head>

<h2 id="bv" align="center"></h2> // where I eventually want to show botVariable

<script>
var socket = io.connect("http://bot.herokuapp.com/"); //this is where I host the bot

socket.on('new_value', function(botVariable) {
console.log(botVariable);
cBotVariable = botVariable; //I'm trying to make cBotVariable a global variable I can refer to on the client side
});

document.getElementById("bv").innerHTML = cBotVariable;
</script>

Result/Error

When I load the site, botVariable fails to show up. I receive the following error in my console.

> GET http://bot.herokuapp.com/socket.io/socket.io.js 
(index):78 

> Uncaught ReferenceError: io is not defined

When I go to bot.herokuapp.com/socket.io/socket.io.js, it says Could not find path: /socket.io/socket.io.js.

I was under the impression that you didn't literally need to supply this file, but that the server somehow creates it on the fly. I guess this is not the case?

Upvotes: 0

Views: 196

Answers (1)

sanchez
sanchez

Reputation: 4530

Try installing socket.io

npm install socket.io

and including it from:

<script src="PATH_TO_NODE_MODULES/socket.io/node_modules/socket.io-client/dist/socket.io.js"></script>

or use CDN:

<script src="https://cdn.socket.io/socket.io-1.2.1.js"></script>

Upvotes: 1

Related Questions