Microsoft recently added support for Socket.IO on Azure in public preview, allowing developers to leverage a fully managed cloud solution through Web PubSub for Socket.IO.
Azure Web PubSub for Socket.IO is a new capability that manages client connections for an application using the Socket.IO library - an open-source library for real-time messaging between clients and a server. Instead of managing multiple Socket.IO servers or adapters in a self-hosted Socket.IO, developers can migrate it to Web PubSub for Socket.IO.
When developers host a Socket.IO app themselves, clients establish WebSocket or long-polling connections directly with their server. Maintaining such stateful connections places a heavy burden on the Socket.IO server – limiting the number of concurrent connections and increasing messaging latency. An option is to scale out to multiple Socket.IO servers. Yet, it requires a server-side component called an adapter, which introduces an extra component a developer needs to deploy and manage, including writing additional code.
By bringing Socket.IO to Azure, Microsoft takes away handling scaling and implementing code logic related to using an adapter from the developers.
Simple Overview of Web PubSub for Socket.IO (Source: Microsoft Learn)
Client code to a client with Web PubSub for Socket.IO would look like:
/*client.js*/
const io = require("socket.io-client");
const socket = io("<web-pubsub-socketio-endpoint>", {
path: "/clients/socketio/hubs/Hub",
});
// Receives a message from the server
socket.on("hello", (arg) => {
console.log(arg);
});
// Sends a message to the server
socket.emit("howdy", "stranger")
While code for a Socket.IO server integrated with Web PubSub for Socket.IO would look like:
/*server.js*/
const { Server } = require("socket.io");
const { useAzureSocketIO } = require("@azure/web-pubsub-socket.io");
let io = new Server(3000);
// Use the following line to integrate with Web PubSub for Socket.IO
useAzureSocketIO(io, {
hub: "Hub", // The hub name can be any valid string.
connectionString: process.argv[2]
});
io.on("connection", (socket) => {
// Sends a message to the client
socket.emit("hello", "world");
// Receives a message from the client
socket.on("howdy", (arg) => {
console.log(arg); // Prints "stranger"
})
Kevin Guo, a product manager at Microsoft, explained in a Socket.IO blog post:
What’s more important is that server and client apps continue using the same and familiar Socket.IO APIs. With only a few lines of code, you can get any socket.io apps running locally to Azure.
In addition, Martin Flower, a distinguished engineer at Microsoft on .NET, tweeted:
We added socket io support natively on Azure! Of course, I prefer SignalR, but for the nodejs apps out there, this can significantly reduce the complexity of scaling your http://socket.io based applications.
Since Socket.IO is hosted in Azure Web PubSub, the pricing and availability details are available on the pricing page.