admin管理员组文章数量:1335130
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
2 Answers
Reset to default 5Depending on the configuration, I can think of three ways.
- if services are node clusters, a fast database like leveldb or redis.
- if services are on the same machine but running as separate processes, then redis or unix sockets work well.
- 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)
本文标签: javascriptShare objects in nodejs between different instancesStack Overflow
版权声明:本文标题:javascript - Share objects in nodejs between different instances - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742374456a2462881.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论