menu
announcement

Spectrum is now read-only. Learn more about the decision in our official announcement.

Truffle Suite

Sweet tools for smart contracts

Channels
Team

How to get the websocket provider from ganache-core/ganache-cli

September 9, 2020 at 4:10pm

How to get the websocket provider from ganache-core/ganache-cli

September 9, 2020 at 4:10pm
Hey guys, so this is what my set up is currently
const ganache = require('ganache-core')
const options = {...ws:true...}
const server = ganache.server(options)
server.listen(8545)
return server.provider
this returns the web3provider instance, is there anyway I can modify the return statement to return the websocket provider? or do I manually have to write in ws://localhost:8545 everytime?
Answer appreciated, thanks

September 9, 2020 at 5:07pm
ganache core/cli host and port are setup by default to localhost(0.0.0.0 for docker) and 8545, you can also change the port using the options object, or -p on the cli. (there is an -h flag for the host also on cli). If you don't want to write it every time you can always set an env var, or use the truffle-config.js when using truffle.
Edited
hmm that isn't really my use-case, I need to return a websocket provider , is that possible?
from what i have read on the ganache's code, it does not use WebsocketProvider as Web3 but their own Provider class. I am not really sure what is the use case, but you can still ganache provider to inject it to the Web3.
const provider = ganache.provider(options)
const web3 = new Web3(provider)
Edited
hope it helps

September 10, 2020 at 11:29am
The docs say that the ws option set to true "enables a websocket server". I assume this means that what is returned is a websocket provider. ws is also true by default.
Did that answer your question? I'm not sure what you mean by "manually write in ..." above, can you elaborate a bit?
hmm the returned provider from server.provider is http