Socket.io with PubNub…why?

前端 未结 1 895
[愿得一人]
[愿得一人] 2021-02-02 13:14

I see that PubNub say they support Socket.io - http://blog.pubnub.com/node-js-supercharged-by-pubnub/#socket.io-github

Can someone explain to me what is going on here be

相关标签:
1条回答
  • 2021-02-02 13:58

    Socket.IO on PubNub Network

    PubNub and Socket.IO are two separate technologies, independent yet connected by the open mobile web.

    PubNub Data Streaming Network

    PubNub is a globally distributed Data Stream Network. Available are simple primitives that make any real-time service possible with High Reliability and Globally Distributed Data Centers.

    Socket.IO Realtime Framework

    Socket.IO is a framework with abstracted concepts that make network communication a little more robust with some great features and use patterns to make it easy. Consider Socket.IO is to Networking as jQuery is to HTML/JavaScript. PubNub is a TCP Socket Cloud. Socket.IO is a framework that has design patterns. Socket.IO is a nice framework on top of PubNub that gives you the some pretty great and easy-to-use design patterns. Socket.IO also has a server-component written in Node.JS which requires you to host your own cluster of servers to operate. Putting Socket.IO on PubNub removes the need to operate and run your own server cluster.

    Also consider that the Socket.IO SDK for PubNub is designed for people that started with socket.io but want to migrate to PubNub. Otherwise, there is no requirement to use socket.io library if you are starting with PubNub first.

    PubNub Removes the need for a server back-end so you can focus on building your apps.

    Also those familiar with Socket.IO API will easily be able to port their existing JavaScript-based Socket.IO code directly onto PubNub - https://github.com/pubnub/javascript/tree/master/socket.io#how-to-use

    Socket.IO Get Started Quickly

    Socket.IO allows you to emit and receive custom events. Reserved Events are: connect, message, disconnect, reconnect, ping, join and leave.

    Sending and receiving events.

    <script src="http://cdn.pubnub.com/socket.io.min.js"></script>
    <script>
    (function(){
        
        // IMPORTANT: PubNub Setup with API Keys
        var pubnub_setup = {
            channel       : 'my_mobile_app',
            publish_key   : 'demo',
            subscribe_key : 'demo'
        };
        
        var socket = io.connect( 'http://pubsub.pubnub.com', pubnub_setup );
        
        socket.on( 'connect', function() {
            console.log('Connection Established! Ready to send/receive data!');
            socket.send('my message here');
            socket.send(1234567);
            socket.send([1,2,3,4,5]);
            socket.send({ apples : 'bananas' });
        } );
        
        socket.on( 'message', function(message) {
            console.log(message);
        } );
        
        socket.on( 'disconnect', function() {
            console.log('my connection dropped');
        } );
        
        // Extra event in Socket.IO provided by PubNub
        socket.on( 'reconnect', function() {
            console.log('my connection has been restored!');
        } );
        
    })();
    </script>
    
    0 讨论(0)
提交回复
热议问题