is a library that allows a server to do server pull and server push. Server Push means the server can send something to the client without the client requesting it. relies on WebSockets for Web Applications.

It's useful to

  • πŸ’ Push notifications to the client
  • ✨ Push events to the client (instead of querying the API periodically for updates, just listen for new events)
  • 🍹 Making chat applications

Or more generally, making real-time applications. The server can push data to the client without being requested (server push).

Basic usage is quite easy to use.

  • Listen to an event: on("eventName", callback)
  • Stop listening to an event: off("eventName")
  • Stop listening to every event: off()
  • Emit an event: emit("eventName", JSON_DATA)
// ex: node client listening to "event"
client.on("event", (data) => {
    // send a reply
    client.emit("event-ack", data)

    // stop listening to event"event")

Express Node.js server

This code targets node.js applications running Express.js. You may use with other libraries such as Angular...

$ npm i

In a project generated with express-generator, append to bin/www

* Create listener
*/ = require('')(server)
// you may remove this later'connection', () => {
    console.log('A new client connected to WebSockets')

See Handling CORS.

Ex: allowing any host to make requests.

- = require('')(server)
+ = require('')(server, {
+    cors: {
+        origin: "*"
+    }

And in app.js, you must add a middleware

app.use((req,res,next) => { = () => (;

Then, in any request, you can fetch io for req.'/message', (req, res) => {'new-message', res.body)

There are many other alternatives, but I like this one because we are using a function, so we don't copy inside every request the io object, and we can use it everywhere from the req object.

Node.js client

You could create a client to test your code

$ npm i
const io = require("");
const client = io("http://localhost:3000");
client.on('connect', () => {

HTML client

You need to add the following script (version 4.5.3). See cdnjs.

<script src="" integrity="sha512-iWPnCISAd/J+ZacwV2mbNLCaPGRrRo5OS81lKTVPtRg1wGTC20Cfmp5Us5RcbLv42QLdbAWl0MI57yox5VecQg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
const client = io("http://localhost:3000");
client.on('connect', () => {

Android client (Kotlin)

See Also

You must add the library in build.gradle

dependencies {
+    implementation ('') {
+        exclude group: 'org.json', module: 'json'
+    }

Then, let's create a handler for our connection.

package xxx

import io.socket.client.IO
import io.socket.client.Socket

object SocketIOHandler {
    private lateinit var _socket: Socket
    val socket: Socket
        get() = _socket

    fun init() {
        try {
            _socket = IO.socket("")
        } catch (_: URISyntaxException) {}

    fun dispose() {

If you don't have one, you need to add a class Application. Inside, you will need to initialize and close client.

+        android:name=".MainApplication"
package xxx

import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ProcessLifecycleOwner
import xxx.SocketIOHandler

class MainApplication : Application(), DefaultLifecycleObserver {
override fun onCreate() {

    override fun onDestroy(owner: LifecycleOwner) {

Then, you're now ready. For instance, in your MainActivity.

SocketIOHandler.socket.on("event") {
    // fetch argument (use JSONArray for [])
    val json = it[0] as JSONObject
    // fetch values { key: value }
    val value = json.getString("key")
    val value = json.getJSONObject("key")
    // ...

Additional notes

  • Execute code when connected
SocketIOHandler.socket.on(Socket.EVENT_CONNECT) {
    /* ... */