socketbridge

SocketBridge is a library that bridges the socket.io and WebSocket.

Usage no npm install needed!

<script type="module">
  import socketbridge from 'https://cdn.skypack.dev/socketbridge';
</script>

README

SocketBridge

SocketBridge is a library that bridges the socket.io and WebSocket.

Usage example

  • WebBrowser <--(socket.io)--> Node.js + SocketBridge <--(WebSocket)--> WebBrowser
  • WebBrowser <--(socket.io)--> Node.js + SocketBridge <--(WebSocket)--> Other Application

Install

npm install socketbridge

Exapmle

Server


var SocketBridgeServer = require('socketbridge').Server;

var socketBridge = new SocketBridgeServer({socketIO: {server: srv}, webSocket: {port:3000}});
socketBridge.on('connection', function(socket) {
    socket.emit('init', {hello: 'world'});

    socket.on('news', function(data) {
        socketBridge.emit('news', data);
    });

    socket.on('message', function(data) {
        socket.broadcast('message', data);
    });

});

Client (WebBrowser: using socket.io)


var socket = io.connect('http://hogehoge.com');
socket.on('connect', function() {
    console.log('connected');
});

socket.on('init', function(data) {
    alert('init');
});

socket.emit('news', 'fugafuga');
socket.emit('message', 'hogehoge');

Note:
When using a port number other than 80

<script src="http://hogehoge:3000/socket.io/socket.io.js"></script>

Client (WebBrowser: using WebSocket)


var socket = new WebSocket('ws://hogehoge.com:3000');
socket.onopen = function() {
    console.log('connected');
};

socket.onmessage = function(e) {
    var data = JSON.parse(e.data);
    if(data.n == 'news') {
        alert('news: ' + data.v);
    } else if(data.n == 'message') {
        alert('message: ' + data.v);
    }
};

Client (Other Application: using WebSocket-Sharp Library)


WebSocket webSocket = new WebSocket("ws://hogehoge.com:3000");

webSocket.OnOpen += (sender, e) => {
    print("connected");	
};

webSocket.OnMessage += (sender, e) => {
    IDictionary data = new (IDictionary) Json.Deserialize(e.Data);
    if(data["n"] == "news") {
        print("news: " + data);
    } else if(data["n"] == "message") {
        print("message: " + data);
    }
};

Usage

Create SocketBridgeServer


var socketBridge = new SocketBridgeServer({socketIO: {server: srv}, webSocket: {port:3000}});

Option


{
        // socket.io Server option
        socketIO: 
        {
            // Create from HTTP server
            server: srv, 

            // OR

            // Create from new server(use port 80)   
            port: 80 
        },

        // WebSocket Server option
        webSocket: 
        {
            // Create from HTTP server
            server: srv,

            // OR

            // Create from new server(use port 3000)
            port: 3000
        }
}

SocketBridge Server API


// Connected
socketBridge.on('connection', function(socket) {
    console.log('Connected');

    // Recieve event
    socket.on('emit', function(msg) {
        // Emits an event to the socket identified.
        socket.emit('message', msg);
    });

    socket.on('emitAll', function(msg) {
        // Emits an event to all connected clients.
        socketBridge.emit('message', msg);
    });

    socket.on('broadcast', function(msg) {
        // Emits an event to all connected clients except this socket.
        socket.broadcast('message', msg);
    });

    socket.on('emitForSio', function(msg) {
        // Emits an event to all connected clients of socket.io connections.
        socketBridge.emitForSocketIO('message', msg);
    });

    socket.on('emitForWs', function(msg) {
        // Emits an event to all connected clients of WebSocket connections.
        socketBridge.emitForWebSocket('message', msg);
    });

    socket.on('broadcastForSio', function(msg) {
        // Emits an event to all connected clients of socket.io connections except this socket.
        socket.broadcastForSocketIO('message', msg);
    });

    socket.on('broadcastForWs', function(msg) {
        // Emits an event to all connected clients of WebSocket connections except this socket.
        socket.broadcastForWebSocket('message', msg);
    });

    //Event called at the time of disconnect.
    socket.on('disconnect', function() {
        console.log('Disconnected!');
    });
});