javascript - Node JS push server send/receive -
i trying set node js server push notifications browser app. have basic example working, wondering how send data server client on handshake.
i need send server user id, when notification comes in them, can routed user.
my server looks this
var app = require('http').createserver(handler) , io = require('socket.io').listen(app) , fs = require('fs'); app.listen(8000); function handler ( req, res ) { res.writehead( 200 ); res.end('node working'); }; io.sockets.on( 'connection', function ( socket ) { socket.volatile.emit( 'notification' , "blah" ); });
and client looks this
var socket = io.connect('http://localhost:8000'); socket.on('notification', function (data) { //prints data here });
in socket.io, emit other event handler (e.g. jquery's .on('click'...)
); declare event , send data. on server, add .on('event', ...)
catch request , process it.
the socket.io front page shows example server:
var io = require('socket.io').listen(80); io.sockets.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); });
and client:
<script src="/socket.io/socket.io.js"></script> <script> var socket = io.connect('http://localhost'); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); }); </script>
it sounds part you're looking socket.emit
portion.
Comments
Post a Comment