An easiest way for migrating from socket.io to PubNub.

Bhavya Dhiman
2 min readNov 14, 2022

--

Socket.io and PubNub both are heavily used for Pub/Sub and/or Real time applications like chat, gaming etc. But in socket.io, you need to manage the backend server yourself for handling real-time data and if not managed properly, it can cause huge network load. Poor management can use upto GBs per second of network traffic which can be very costly in the long run. But in PubNub, it is handled automatically.

Users who want to migrate from socket.io to PubNub, it won’t be any easy task if you are using pubnub sdk. You can do using socket.io as well.

Here is an example from PubNub docs that how to easliy migrate without changig the socket.io code as well. You just need to change url and the configuration shown in the example code below:

<script src="http://cdn.pubnub.com/socket.io.min.js"></script>
<script>(function(){
// IMPORTANT: PubNub Setup with Account
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>

It will be very quick for users who are using socket.io and want to switch to PubNub.

Reference: https://www.pubnub.com/blog/pubnub-takes-socketio-next-level/

--

--

No responses yet