visit
WebSocket. A common topic among developers learning their way around (just see the number of ELI5s on Reddit for yourself), and a fantastic way to exchange data between a server and a web or a mobile interface. Chats, page-refreshing, event notifications, game consoles, loads of other things — all require a via WebSocket.
A short while ago we made an article on how to set up a backend for React apps in just 15 minutes. Setting up the WebSocket connection is the next step to making an awesome app with live updates within the same framework.
↑ The crux of what WebSocket is famous for
This bite-sized article will dive into what WebSocket really is, why it’s a good idea to use it, and how it .Step 1
Explore what Directual platform is and connect it with your ReactJS-app. Check out this short video to get an idea of how to get around Directual and what you can do:
Step 2
Install the Socket.io plugin on Directual (free of charge, by the way!)Step 3
Configure the scenario and establish the logic for your WebSocket. Make sure your scenario sends the WebSocket at the right time.
Use the following code in your React app to handle the messages from the scenario. There are two arguments:
Event type
and message
. You may either broadcast the message (set UserID
to *
in the scenario step) or send it to a certain user (authorised one).import io from 'socket.io-client';
// autoConnect set to false,
// because before we must get session_id for auth users
const socket = io('//api.directual.com', {autoConnect: false})
var sessionID = ? // get sessionID when authorise a user
var appID = ? // get appID in API section of the Directual platform
//login users process...
socket.auth = {app_id: appID, session_id: sessionID};
socket.connect();
//subscribe to any events
socket.onAny((eventName, args) => {
console.log(`${eventName}: ${JSON.stringify(args)}`);
});
//subscribe to only 'message' events
socket.on('message', (msg) => {
console.log(msg);
})
Actually, you can use Directual WebSocket plugin not only for ReactJS, but for any UI-framework, or even for a mobile app!
Naturally, the platform creators themselves are always there to respond. Send us a message at . Thanks for reading!