Creating Socket IO channels from the client side

I am new to Nodejs and Socketio. I want to do something like the following.

-> Create a socket.io/node.js server that listens on the channels specified in the web browser. -> server side script pushes messeges to specific channels.

(I already have a server set up and here is the code. This just sends two messeges: action and messege for all connected clients)

//start code    
var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , url = require('url')

app.listen(8080);

function handler (req, res) {
    // parse URL
    var requestURL = url.parse(req.url, true);

    // if there is a message, send it
    if(requestURL.query.message)
            sendMessage(decodeURI(requestURL.query.action), decodeURI(requestURL.query.message));

    // end the response
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end("");
}

function sendMessage(action, message) {
    io.sockets.emit('notification', {'action': action, 'message' : message});
}

      

So basically the Socket.io server doesn't have pipes defined inside them. But they are defined by the client (js in the browser) and messeges are sent to specific channels on the server side script (like php using cURL)

Sorry if asked questions before, I searched and didn't find anything useful.

+3


source to share


1 answer


I am a bit confused about your question. By pipes, do you mean socket.io rooms? https://github.com/LearnBoost/socket.io/wiki/Rooms
Rooms are groups of customers and you can send a message to all customers in a room using the following command:

io.sockets.in('room').emit('event_name', data)

      

It is important to understand that rooms are server rooms. Therefore, if you want to send a message to a room from a client, you must send a message (or request) to the server name and the passage name.



Also, it's a bit unusual for your handler function to appear as an HTTP endpoint. If your clients already have socket.io connections, the easier it is to send it as a socket.io message.

If that doesn't answer your question, can you post your client side code as well? Maybe this will help me understand what you are trying to achieve.

+1


source







All Articles