How do abstract Socket.IO connections in your SPA.

When working with socketIO client in your SPA, it may become very hard to manage the socket instance, events emitters, and work with the callbacks in different places of the app. Especially when there are multiple different servers to connect with. Wha…


This content originally appeared on DEV Community and was authored by Huzaifa Rasheed

When working with socketIO client in your SPA, it may become very hard to manage the socket instance, events emitters, and work with the callbacks in different places of the app. Especially when there are multiple different servers to connect with. What can be done?

Solution

We can create a closure that takes event callbacks as functions and return event emitters, to abstract socketIO implementation in a single file (let's call it SocketManager.js).

Example

import io from "socket.io-client";

const SocketManager = ({
  onCallback = () => {},
}) => {
  const socket = io(/* server url to connect */);

  // Callbacks
  socket.on('callback-event-name', (payload) => {
    onCallback && onCallback(payload);
  });

  // Emitters
  const emitEvent = (eventPayload) => {
    socket.emit('emit-event-name', eventPayload);
  };

  return {
    socket,
    emitEvent
  };
};

export default SocketManager;

then we can use this in our React code like this

import React from "react";
import SocketManager from "./SocketManager.js";

const SocketConsumer = () => {
  const { emitEvent } = SocketManager({
    onCallback: handleCallback,
  });

  const handleCallback = (payload) => {
    /** Do something with the payload */
  };

  return <button onClick={emitEvent}>Fire Event</button>;
};

export default SocketConsumer;

Hope this helps you in your projects. Thanks 🙂


This content originally appeared on DEV Community and was authored by Huzaifa Rasheed


Print Share Comment Cite Upload Translate Updates
APA

Huzaifa Rasheed | Sciencx (2022-02-10T18:49:58+00:00) How do abstract Socket.IO connections in your SPA.. Retrieved from https://www.scien.cx/2022/02/10/how-do-abstract-socket-io-connections-in-your-spa/

MLA
" » How do abstract Socket.IO connections in your SPA.." Huzaifa Rasheed | Sciencx - Thursday February 10, 2022, https://www.scien.cx/2022/02/10/how-do-abstract-socket-io-connections-in-your-spa/
HARVARD
Huzaifa Rasheed | Sciencx Thursday February 10, 2022 » How do abstract Socket.IO connections in your SPA.., viewed ,<https://www.scien.cx/2022/02/10/how-do-abstract-socket-io-connections-in-your-spa/>
VANCOUVER
Huzaifa Rasheed | Sciencx - » How do abstract Socket.IO connections in your SPA.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/10/how-do-abstract-socket-io-connections-in-your-spa/
CHICAGO
" » How do abstract Socket.IO connections in your SPA.." Huzaifa Rasheed | Sciencx - Accessed . https://www.scien.cx/2022/02/10/how-do-abstract-socket-io-connections-in-your-spa/
IEEE
" » How do abstract Socket.IO connections in your SPA.." Huzaifa Rasheed | Sciencx [Online]. Available: https://www.scien.cx/2022/02/10/how-do-abstract-socket-io-connections-in-your-spa/. [Accessed: ]
rf:citation
» How do abstract Socket.IO connections in your SPA. | Huzaifa Rasheed | Sciencx | https://www.scien.cx/2022/02/10/how-do-abstract-socket-io-connections-in-your-spa/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.