CrastyCrap's avatar

Best way to deal with websocket

Hey guys,

Currently, I am working with a small team on a new project that is supposed to be similar to Asana. We are developing it using Vue 3 and Laravel (with GraphQL) on the backend. The board in our application is intended to be a live board, and we are using Pusher for real-time updates. However, we have found that when a user makes an update, it takes 4 to 7 seconds for other users to receive it, which is a significant time gap compared to ClickUp's 1 to 2 seconds.

We are exploring better solutions. Currently, we have two potential options. First, we suspect that using an external server like Pusher might be causing the delay. Second, we are considering sending updates through a service like Firebase or using a built-in Socket.io server. According to you, what is the better solution here? Also, if you have any other solutions, could you tell us about them?

0 likes
2 replies
LaryAI's avatar
Level 58

To address the issue of real-time updates taking 4 to 7 seconds to propagate, there are several potential solutions you can explore. Here are some suggestions:

1. Self-Hosted WebSocket Server with Socket.io

Using a self-hosted WebSocket server with Socket.io can give you more control over the latency and performance of your real-time updates. This approach eliminates the dependency on third-party services like Pusher, which might introduce additional latency.

Steps to Set Up a Self-Hosted WebSocket Server with Socket.io:

  1. Install Socket.io: First, you need to install Socket.io on your server. You can do this using npm.

    npm install socket.io
    
  2. Set Up the Server: Create a basic server using Node.js and Socket.io.

    const express = require('express');
    const http = require('http');
    const socketIo = require('socket.io');
    
    const app = express();
    const server = http.createServer(app);
    const io = socketIo(server);
    
    io.on('connection', (socket) => {
        console.log('a user connected');
        socket.on('disconnect', () => {
            console.log('user disconnected');
        });
    
        socket.on('update', (data) => {
            io.emit('update', data);
        });
    });
    
    server.listen(3000, () => {
        console.log('listening on *:3000');
    });
    
  3. Client-Side Integration: Integrate Socket.io on the client side (Vue 3).

    import { io } from "socket.io-client";
    
    const socket = io('http://localhost:3000');
    
    socket.on('connect', () => {
        console.log('connected to server');
    });
    
    socket.on('update', (data) => {
        // Handle the update
        console.log('Update received:', data);
    });
    
    // Emit an update
    socket.emit('update', { /* your update data */ });
    

2. Optimize Pusher Configuration

If you prefer to stick with Pusher, you can try optimizing your Pusher configuration to reduce latency. Ensure that you are using the closest Pusher cluster to your users and that your server is also geographically close to the Pusher cluster.

3. Use Firebase Realtime Database

Firebase Realtime Database can be another alternative for real-time updates. It is known for its low latency and ease of integration.

Steps to Use Firebase Realtime Database:

  1. Set Up Firebase: Create a Firebase project and add your web app to it.

  2. Install Firebase: Install Firebase in your project.

    npm install firebase
    
  3. Initialize Firebase: Initialize Firebase in your Vue 3 application.

    import { initializeApp } from "firebase/app";
    import { getDatabase, ref, onValue, set } from "firebase/database";
    
    const firebaseConfig = {
        apiKey: "YOUR_API_KEY",
        authDomain: "YOUR_AUTH_DOMAIN",
        databaseURL: "YOUR_DATABASE_URL",
        projectId: "YOUR_PROJECT_ID",
        storageBucket: "YOUR_STORAGE_BUCKET",
        messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
        appId: "YOUR_APP_ID"
    };
    
    const app = initializeApp(firebaseConfig);
    const database = getDatabase(app);
    
    // Listen for updates
    const updatesRef = ref(database, 'updates');
    onValue(updatesRef, (snapshot) => {
        const data = snapshot.val();
        console.log('Update received:', data);
    });
    
    // Send an update
    set(updatesRef, { /* your update data */ });
    

Conclusion

Each of these solutions has its pros and cons. A self-hosted Socket.io server gives you full control over the real-time communication but requires more setup and maintenance. Optimizing Pusher or using Firebase can be easier to implement but might still have some latency depending on various factors.

Evaluate these options based on your team's expertise, project requirements, and the desired level of control over the real-time communication.

Please or to participate in this conversation.