Skip to content

Instantly share code, notes, and snippets.

@crtr0
Created June 8, 2012 17:02
Show Gist options
  • Save crtr0/2896891 to your computer and use it in GitHub Desktop.
Save crtr0/2896891 to your computer and use it in GitHub Desktop.

Revisions

  1. crtr0 revised this gist Jun 8, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion server.js
    Original file line number Diff line number Diff line change
    @@ -13,5 +13,5 @@ io.sockets.on('connection', function(socket) {
    room = "abc123";
    io.sockets.in(room).emit('message', 'what is going on, party people?');

    // this message will NOT go to the client defined below
    // this message will NOT go to the client defined above
    io.sockets.in('foobar').emit('message', 'anyone in this room yet?');
  2. crtr0 created this gist Jun 8, 2012.
    14 changes: 14 additions & 0 deletions client.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    // set-up a connection between the client and the server
    var socket = io.connect();

    // let's assume that the client page, once rendered, knows what room it wants to join
    var room = "abc123";

    socket.on('connect', function() {
    // Connected, let's sign-up for to receive messages for this room
    socket.emit('room', room);
    });

    socket.on('message', function(data) {
    console.log('Incoming message:', data);
    });
    17 changes: 17 additions & 0 deletions server.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    // attach Socket.io to our HTTP server
    io = socketio.listen(server);

    // handle incoming connections from clients
    io.sockets.on('connection', function(socket) {
    // once a client has connected, we expect to get a ping from them saying what room they want to join
    socket.on('room', function(room) {
    socket.join(room);
    });
    });

    // now, it's easy to send a message to just the clients in a given room
    room = "abc123";
    io.sockets.in(room).emit('message', 'what is going on, party people?');

    // this message will NOT go to the client defined below
    io.sockets.in('foobar').emit('message', 'anyone in this room yet?');