opkfront.blogg.se

Socket io express
Socket io express












⚠ This is extremely important to note since both app and server instances have completely separate functions. After the app instance is created from Express NodeJS library, a server instance which wraps around the app instance is initialised via: const server = http.createServer(app) The custom namespaces bus_arrivalsand get_bus_arrivals_info shall subsequently correspond to the exact same namespaces (KIV) from the client-side such that in the event multiple socket channels are present, the web socket instance would be able to identify based on namespaces which messages to emit to which channels.Ĥ*. Any web socket instance has by default the namespace disconnect- which emits a message back to the server if the instance has disconnected from the frontend (usually due to disconnection from its web server)ģ. The library socket.io is imported via: const socketio=require("socket.io")Ģ.

socket io express

So there are a few crucial points that should be noted in the above code snippet:ġ. After installing the NodeJS Socket.io library via npm install socket.io, the server-side code implementation is as follows: the entry point of the web application where an instance of Express.js is being initialised. Server-Side Implementation of Socket.ioįor starters, within an Express NodeJS web app, the server-side of the code conventionally refers to the app.js/server.js file i.e. While there are alternative libraries which are capable of integrating real-time data streaming within a web app, my primary reason for implementing Socket.io is because it not only serves its functionality of establishing a Web socket bidirectional connection between Server + Client but also, both Server & Client code implementations are written in JavaScript so there is no need to switch back and forth between 2 languages while coding the application’s backend and frontend. In short, I was curious about what other data fields the API includes in its real-time responses 🤔

socket io express socket io express

I later decided to further explore and leverage on the data provider’s public API which includes returning the real-time bus arrival timings at any specific bus stop. Screenshot by Author | The Origin-Destination bus route data selected can be exported on the web app














Socket io express