最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS SocketIO singleton - Stack Overflow

programmeradmin1浏览0评论

i'm working with socket io, and would like to apply singleton pattern in order to keep the same instance of socket between different file

both main.js & device.js are loaded when on mobile, but have different socket instance

Main.js :

import ioInstance from './socket'    
console.log(ioInstance)

Device.js :

import ioInstance from './socket'
console.log(ioInstance)

socket.js :

import io from 'socket.io-client'
export const ioInstance = io('http://192.168.1.15:1337')

i've tried many ways of doing this but i always have different socket instance ...

Anyone having any clue on how to do it right ? Thanks

i'm working with socket io, and would like to apply singleton pattern in order to keep the same instance of socket between different file

both main.js & device.js are loaded when on mobile, but have different socket instance

Main.js :

import ioInstance from './socket'    
console.log(ioInstance)

Device.js :

import ioInstance from './socket'
console.log(ioInstance)

socket.js :

import io from 'socket.io-client'
export const ioInstance = io('http://192.168.1.15:1337')

i've tried many ways of doing this but i always have different socket instance ...

Anyone having any clue on how to do it right ? Thanks

Share Improve this question asked Dec 24, 2017 at 23:17 HervéHervé 411 silver badge4 bronze badges 8
  • What is your execution environment? Browser? node.js? Electron? – jfriend00 Commented Dec 24, 2017 at 23:25
  • I'm on ubuntu, running this with express server and Chrome as browser – Hervé Commented Dec 24, 2017 at 23:28
  • Is this code running in node.js with ubuntu or is this Chrome browser code? – jfriend00 Commented Dec 24, 2017 at 23:38
  • This is the front end part of my small app – Hervé Commented Dec 24, 2017 at 23:39
  • Indeed, i still can't see how to apply a singleton pattern to use the same instance in different file. Something else than just attaching my socket to my javacript window object – Hervé Commented Dec 24, 2017 at 23:51
 |  Show 3 more ments

1 Answer 1

Reset to default 5

Your actual singleton code is not operating as a singleton at all. When you have this:

const socketConnection = {
  _instance: null,
  get instance () {
    if (!this._instance) {
      this._instance = {
        setConnection () {
          return io('http://192.168.0.11:1337')
        },
      }
    }
    return this._instance
  }
}

And you are referencing:

socket.instance.setConnection()

That is calling io('http://192.168.0.11:1337') every time you reference setConnection(), even when this._instance is already set. That's the problem. Instead, you need to save or cache in the instance the result of calling io('http://192.168.0.11:1337') so you only ever call it once.


You could fix it by changing to this:

const socketConnection = {
  _instance: null,
  get instance () {
    if (!this._instance) {
      this._instance = io('http://192.168.0.11:1337');
    }
    return this._instance;
  }
}

And, then just using this to reference the singleton socket:

import socket from './socket'
console.log(socket.instance);
socket.instance.on(...);

You could also simplify matters and just have this:

import io from 'socket.io-client'

const socketConnection = io('http://192.168.0.11:1337');

export default socketConnection;

And, then just use this to reference the singleton socket:

import socket from './socket'
console.log(socket);
socket.on(...);

FYI, we probably would have solved this in minutes after posting your question if you showed the actual singleton code in your original question. It's always better here to show your actual code than the try to make up a pseudo code example because the problem is often not exactly where you think so we need to see the real code to identify the actual solution.

发布评论

评论列表(0)

  1. 暂无评论