Ad

Event On.('event'.... Never Fired, Why?

- 1 answer

I am doing collaboration task with socket.io, for that I use event.

in my server server.js i have :

io.sockets.on('connection', function (socket) {
    console.log("socketon");
    socket.on('task', function(task){
        console.log("another task");
        task=ent.encode(task);
        socket.broadcast.emit('task', {task : task});
    })
});

and on my view i have shared.ejs I have :

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

socket.on('task', function (data) {
    printtask(data.task);
})

$('#addtask').submit(function () {
    let task = $('#task').val();
    socket.emit('task', task);
    printtask(task);
    $('#task').val('').focus();
    return false;
});

function printtask(task) {
    $('#todolistlist').prepend("<li> <a class='text-danger' href='#'>✘</a>" + task + '</li>');
}

Socket is intalled and work fine when i go to the page http://localhost:8080/shared/ i have in console "socketon" however when i click on my submit I haven't "another task" in console.
I don't know why my event "task" is not fired.
If someone have an idea I would be glad :)
PS: in case of minus please let me know why so that I can upgrade my post
EDIT:
here the requirement passed in server.js :

var express = require('express');
var app = express();
let server = require('http').createServer(app);
let ent = require('ent');
var session = require('cookie-session');
var bodyParser = require('body-parser');
let io = require('socket.io').listen(server);
var encodedUrl = bodyParser.urlencoded({ extended: false });

Here the different get (my socket is used in /shared) :

.get('/', function(req, res) {
    res.render('index.ejs');
})
.get('/private/', function(req, res) {
    res.render('todolist.ejs', {todolist: req.session.todolist});
})
.post('/private/add/', encodedUrl, function(req, res) {
    if (req.body.addtodo !=''){
        req.session.todolist.push(req.body.addtodo);
    }
    res.redirect('/private/');
})
.get('/private/delete/:index', function(req, res) {
    if (req.param.index != '') {
        req.session.todolist.splice(req.params.index, 1);
    }
    res.redirect('/private/');
})
.get('/shared/', function (req, res) {
    res.render('shared.ejs');
  })
.use(function(req, res, next){
    res.redirect('/')
});

here the html content of my shared.ejs

<div class='container'>
                <div class= row>
                    <ul id="todolistlist" class="offset-lg-2 col-lg-8 list-group list-group-flush">
                    </ul>
                    <section class="offset-lg-1 col-lg-1">
                        <p>connected:</p>    
                        <ul id="todolistmembers" class="list-group list-group-flush">
                        </ul>
                    </section>
                </div>
                <form id='addtask' method='post' class="form-inline offset-lg-2 mb-3 col-lg-8 align-items-center">
                    <div class="input-group w-100" >
                        <div class="input-group-prepend">
                            <label id="basic-addon3" class="input-group-text" for="addtodo">What to do ?</label>
                        </div>
                        <input type="text" id="task" class="form-control" style="text-align:right" name="addtodo" id="addtodo" autofocus />
                        <span class="input-group-append">
                            <input type="submit" class="btn btn-primary" value="add"/>
                        </span>
                    </div>
                </form>
            </div>
            <script src="/js/jquery.min.js"></script>
            <script src="/socket.io/socket.io.js"></script>
            <script src="/js/bootstrap.min.js"></script>
Ad

Answer

Connecting to 'http://localhost:8080/shared/' instead of 'http://localhost:8080' is connecting to a specific socket.io namespace named shared. So your events are going to be shown on the server only to that namespace. So, unless you register your event handlers on the server in that namespace or remove the /shared from the connection URL, you won't see the events.

Ad
source: stackoverflow.com
Ad