- ReferenceError: socket is not defined


I'm trying to write a simple app that mirrors each character I type in a text area onto a div using, but I keep getting the following client error: "ReferenceError: socket is not defined"

Here's my server code:

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


app.get('/', function(req,res){
    res.sendfile(__dirname+ '/index.html');

io.sockets.on('connection', function(socket){

    socket.on('keyup', function(data){


Client code:

    <div id="output"></div>
    <textarea id = "input"></textarea>

    <script src=""></script>
    <script src="/"></script>


                content = $('#input').val();
                socket.emit('keyup', content);


            socket.on('keydisplay', function(data){


Any leads? What am I doing wrong?

Problem courtesy of: Eugene


Well, isn't that obvious? You haven't defined socket in your client code (that's what ReferenceError means):

    var socket = io.connect('http://localhost');
    // the other code goes here

Note that io is a global variable in script.

Solution courtesy of: freakish


There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.