coffeedjason
coffeedjason

Reputation: 21

socket.io data from server to client

server.js

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

app.use(express.static(__dirname + '/public'));

io.on('connection', function(client) {
    console.log('Client connected...');

client.on('join', function(data) {
    console.log(data);
    io.emit('messages', 'Hello');

});

});

index.html

<script>
    var socket = io.connect('http://localhost:7777');
    socket.on('connect', function(data) {
      socket.emit('join', 'Hello World from client');

    });
    socket.on('messages', function(data) {
      alert(data);
    });
</script>

I tried to implement very basic of Socket.io.

However, data sending from client to server is available but from server to client doesn't work.

In the command running server.js, 'Hello World from client' is printed. However, alert window doesn't work in the web browser.(I've also tried to console.log).

How to solve this?

Editted

I've put server.js codes in the app.get('/', function(req, res)){ ... } Then, it doesn't work. Why it doesn't work in app.get?

Upvotes: 2

Views: 12252

Answers (3)

Aatithya Hitkar
Aatithya Hitkar

Reputation: 1

   socket.on("message",function (reply_data) {
        console.log('inside on message functions ')
        console.log(reply_data);
    })

please change 'messages' to "message" that worked for me

Upvotes: 0

sidzaky
sidzaky

Reputation: 57

Try this, I hope it works:

io.on('connection', function(client) {
console.log('Client connected...');

client.on('join', function(data) {
        console.log(data);
        io.emit('join', data);  //this code sending data from server to client
    });
});

Upvotes: 2

jfriend00
jfriend00

Reputation: 707328

If you're just trying to fetch some data with an Ajax call such as /test, then there is no need to use socket.io. That's just a classic request/response.

app.get('/test', function(req, res) {
    // collect your data and then send it as a response
    res.json(data);
});

If you're just trying to incorporate data into a web page that is requested, then you can use res.render() with the template engine of your choice (ejs, handlebars, pug, etc...). That would typically look like this:

app.get('/test', function(req, res) {
    // collect your data and then pass it to res.render() to render your
    // your template using that data
    res.render('someTemplateName', data);
});

The main thing that socket.io is useful for is "pushing" data from server to client without a client request. So, if something happened on the server that the client was not aware of and the server wanted to tell the client about it, then socket.io would be used for that. The classic example is a chat app. Person A sends a chat message to the server that is addressed to Person B. The server receives that message and then needs to "push" it to Person B. That would be perfect for an already connected socket.io connection because the server can just push the data directly to the Person B client, something the server can't do with request/response (since there is no request from person B).


If you still think you need socket.io, then please describe exactly what you're trying to do with it (step by step what you're trying to send to the client).

Upvotes: 1

Related Questions