3

I've created a simple chat application as recommended (which works fine), but I'm having trouble figuring out how to launch a jQuery-function across web sockets with Socket.IO.

For example, I have a button to click:

<button id="button">
  Click me!
</button>

I would like my the button to execute a function when it's clicked:

$("button").click(function() {
  doSomething();
});

Here is an example of what I have on the client side

var socket = io.connect('http://localhost:3000');

function doSomething() {
    $('body').css({
         background: "red"
    });
}

Here is what I have on the server side (my server.js file) - I think this might be causing part of the issue:

var io = require('socket.io'),
    connect = require ('connect');

var app = connect().use(connect.static('public')).listen(3000);
var commands = io.listen(app);

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

    // Possibly insert handler here?

});

Just in case I wasn't clear on the purpose - I'd like that when the button is clicked, to have the body background of all connected clients turn red.

1 Answer 1

4

You have to emit an event to your node.js/socket.io-server first on button clicked. Then on server-side you have to emit to all clients. On client side you have to listen on that server-side event. Also your server doesn't look very well.

Try something like this for example:

"use strict";

var server = require('http').createServer();
var io = require('socket.io')(server);


io.on('connection', function (socket) {
    //senden an socket dass er verbunden ist
    //socket.emit('userOnline', {message: 'verbunden'});

    //Informationen vom User holen
    socket.on('changeBgColor', function (color) {
        console.log(color);
        socket.broadcast.emit('changeBgColorEveryWhere', color.color);
        socket.emit('changeBgColorEveryWhere', color.color);
    });
});

server.listen('3000');

For example on client-side:

<!doctype html>
<html>
    <head>
        <title>test</title>
    </head>
    <body>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script src="http://localhost:3000/socket.io/socket.io.js"></script>
        <script type="text/javascript">
            var socket = io('ws://localhost:3000/');

            $(document).off('click', '#button').on('click', '#button', function (e) {
                console.log('click');

                socket.emit('changeBgColor', {color: 'red'});
                e.preventDefault();
            });

            socket.on('changeBgColorEveryWhere', function (color) {
                console.log(color);
                $('body').css('background', color);
            });
        </script>

        <button id="button">Button</button>
    </body>
</html>
Sign up to request clarification or add additional context in comments.

2 Comments

When I try this, the page does not load at all, even with the server running.
@DRBC see my updated answer. I tested this on my local machine and it works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.