Client to client communitcation
PieSocket supports client-to-client communications. Which means you can publish messages directly from a client and build a serverless application. You need to enable C2C (client-to-client) communications for your API key from your PieSocket account, for this to work.
There are two ways to publish from a browser.
1. Using PieSocketJS
Include PieSocketJS into your application using NPM
npm install piesocket-js
Copy
or, using CDN
<script src="https://unpkg.com/piesocket-js@1"></script>
Copy
Then, use the following code to publish:
var piesocket = new PieSocket({
clusterId: 'YOUR_CLUSTER_ID',
apiKey: 'YOUR_API_KEY'
});
//Join room and publish
piesocket.subscribe(roomId).then(room => {
room.on("open", ()=>{
room.send("Hello World!");
});
});
Copy
2. Publish using the WebSocket client
var apiKey = 'oCdCMcMPQpbvNjUIzqtvF1d2X2okWpDQj4AwARJuAgtjhzKxVEjQU6IdCjwm'; //Demo key, Change this to yours
var roomId = 1;
var piesocket = new WebSocket(`wss://demo.piesocket.com/v3/${roomId}?api_key=${apiKey}¬ify_self`);
piesocket.send("Hello world!");
Copy
**Help**
Facing difficulties? Use the chat box on the bottom-right corner of this page to reach us.
On this page
