Skip to main content

Socket IO

EzBackend uses socket.io for realtime functionality under the hood.

Accessing Socket IO

You can access the socket io instance from any EzApp or class that extends EzApp, for example EzBackend

From Request

You can get the non-namespaced io object from req

const socketIOtester = new EzApp()

socketIOtester.post('/hello-world', async (req, res) => {
const io = req.io
{/* Your Custom Functionality */}
})

Non Namespaced

You can get the equivalent of the io object from any EzApp with

app.useSocketIORaw((io) => {/* Your Custom Functionality */})

Namespaced

An EzApp's prefix affects the namespacing of subsequent commands. the below io instance is under the namespace /users

users.useSocketIO((io) => {/* Your Custom Functionality */})
app.addApp(users, {prefix: "users"})

For more information on socket io namespacing refer to the socket io docs

Why Socket IO

We chose socket.io instead of websockets or long polling because it is

  1. More opinionated. Socket IO comes with the concept of namespaces and rooms, meaning that you don't have to come up with your own system that is probably not as good
  2. Browser Support. Socket IO comes with fallbacks to long polling if the browser does not support websockets, which in turn means that EzBackend can support requests from more browsers.