javascript - Nodejs server unable to detect connection with Pubnub+SocketIO -
javascript - Nodejs server unable to detect connection with Pubnub+SocketIO -
my nodejs server unable observe when new browser connects ('connection' event) , dont know why. narrowed downwards problem working on few days , suspect has due add-on of pubnub socket connection implemented on browser.
the next server.js
var http = require('http') , connect = require('connect') , io = require('socket.io') , fs = require('fs') , uuid = require('node-uuid') , _ = require('lodash'); // pubnub!!! (how initialize utilize on server) var pubnub = require('pubnub').init({ channel: "my_channel", publish_key: "pub-key", subscribe_key: "sub-c-key", uuid: "server", origin : 'pubsub.pubnub.com' }); pubnub.subscribe({ channel: 'my_channel', callback: function(message) { console.log("message received: ", message); }, message: 'server ready', presence: function(data) { console.log("presense: ", data); }, connect: publish }); // various socket.on() events omitted var app = connect().use(connect.static(__dirname)).use(connect.directory(__dirname)); var server = http.createserver(app); server.listen(8888); io = io.listen(server); io.sockets.on('connection', handlenewpeer); upon arriving on html page, doconnect(isbroadcaster) function ran script tag
the doconnect function (in peer.js):
var doconnect = function(isbroadcaster) { console.log("doconnect"); // broadcaster or normal peer var user; if (isbroadcaster) user = "broadcaster"; else user = "viewer"; (function() { var pubnub_setup = { channel: "my_channel", publish_key: "pub-c-key", subscribe_key: "sub-c-key", user: user }; // note removed var socket = io.connect( 'http://pubsub.pubnub.com', pubnub_setup); // various socket.on() omitted })(); here how before socketio & working:
var doconnect = function(isbroadcaster) { socket = io.connect(); // various socket.on() omitted } my p2p video website implemented webrtc running on nodejs + socketio server. have been trying incorporate pubnub & thought easy since pubnub supports socketio (or @ to the lowest degree client side?). did not think hard set server side.
any input @ on this? think it's simple cannot set finger on
socket.io on server using node.js
socket.io pubnub not provide node.js socket.io backend option. can utilize pubnub sdk straight on-connect events.
npm packagenpm install pubnub after install pubnub npm can utilize node.js server backend:
node.js backend code// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- // pubnub!!! (how initialize utilize on server) // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- var pubnub = require('pubnub').init({ publish_key : "pub-key", subscribe_key : "sub-c-key", uuid : "server-id" }); // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- // on user connect // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- function on_user_connect(data) { console.log( "user connected: ", data.uuid ); } // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- // on user leave // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- function on_user_leave(data) { console.log( "user left: ", data.uuid ); } // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- // open socket connection user bring together events // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- pubnub.subscribe({ channel : 'my_channel', connect : connected message : function(message) { console.log("message received: ", message); }, presence : function(data) { if (data.action == "leave") on_user_leave(data); if (data.action == "timeout") on_user_leave(data); if (data.action == "join") on_user_connect(data); } }); function connected() { console.log('connected!'); } javascript node.js socket.io webrtc pubnub
Comments
Post a Comment