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

javascript - Share objects in nodejs between different instances - Stack Overflow

programmeradmin0浏览0评论

I need to share some objects or plete modules between different node processes or clusters, i searched many blogs without getting the satisfied answer.

If that's impossible to do then any ideas how to overhead that?

I need to share some objects or plete modules between different node processes or clusters, i searched many blogs without getting the satisfied answer.

If that's impossible to do then any ideas how to overhead that?

Share Improve this question edited Sep 14, 2024 at 13:44 João Pimentel Ferreira 16.4k12 gold badges96 silver badges129 bronze badges asked Aug 27, 2014 at 5:11 Akram Kamal QassasAkram Kamal Qassas 5097 silver badges22 bronze badges 2
  • both process can have transaction via net server, http server or stdstram if run as child forked..... (?) – DeckyFx Commented Aug 27, 2014 at 9:17
  • Not sure how that can help – Akram Kamal Qassas Commented Aug 27, 2014 at 10:35
Add a ment  | 

2 Answers 2

Reset to default 5

Depending on the configuration, I can think of three ways.

  1. if services are node clusters, a fast database like leveldb or redis.
  2. if services are on the same machine but running as separate processes, then redis or unix sockets work well.
  3. if services are spread across multiple machines, then scuttlebutt provides a good solution.

redis: Using a fast local database like redis to share objects between processes on the same machine involves using a simple put( key, data ) and get( key ) but you have to serialize and deserialize, probably using JSON. There is no formal concurrency control so you have to take care that any object changes are available to all clients. One way is to use redis built in pub/sub to trigger when changes are made. Another way is to use optimistic locking.

unix sockets For projects that need to share objects from differing technologies, say between a c++ app and node, unix sockets provide a very fast solution. You still have to serialize/deserialize and control concurrency, but it's faster than the database solution and works across different technologies. There is a node project that helps with some of the details.

scuttlebutt Probably the best way to share data between processes on separate machines. From the scuttlebutt site:

Scuttlebutt is intended to be subclassed into a variety of data-models. Two implementations are provided as examples scuttlebutt/model and scuttlebutt/events...

There is also a good white paper that addresses the science behind using replication to share object data.

I hope this gives you some viable alternatives.

I saw your answer about 4 or 5 days ago and decided to make something to do JUST that

https://npmjs./package/webject
This package enables the sharing of objects across the web
An object can be hosted through serve or connected to by connect

Getting the 2 functions like

let {serve,connect}=require('webject')

Someone serving can be like

let myWebject=serve(myObj) //object myObj gets shared, myObj could even be cyclic
let clientToken=myWebject.addToken(1) //level 1 is view only(edits won't edit your server side object)

Someone connecting can be like

let myClientObj=await connect('wss://example.:8009',clientToken) //assuming you shared a client token with someone to connect
//this is an asynchronous promise that if you have a valid location and token, you would connect to an object hosted(by that location)
发布评论

评论列表(0)

  1. 暂无评论